说我有一个字符串
string var = "This is a test";
Run Code Online (Sandbox Code Playgroud)
然后我想使用这个字符串来创建一个XElement实例,如:
XElement element = XElement.Load(var);
Run Code Online (Sandbox Code Playgroud) 我需要能够创建一个如下所示的XML Document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rootprefix:rootname
noPrefix="attribute with no prefix"
firstprefix:attrOne="first atrribute"
secondprefix:attrTwo="second atrribute with different prefix">
...other elements...
</rootprefix:rootname>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
doc.AppendChild(declaration);
XmlElement root = doc.CreateElement("rootprefix:rootname", nameSpaceURL);
root.SetAttribute("schemaVersion", "1.0");
root.SetAttribute("firstprefix:attrOne", "first attribute");
root.SetAttribute("secondprefix:attrTwo", "second attribute with different prefix");
doc.AppendChild(root);
Run Code Online (Sandbox Code Playgroud)
不幸的是,我得到的带有第二个前缀的第二个属性根本没有前缀.它只是"attrTwo" - 就像schemaVersion属性一样.
那么,有没有办法在C#的根元素中为属性设置不同的前缀?