相关疑难解决方法(0)

如何从字符串创建XElement?

说我有一个字符串

string var = "This is a test";
Run Code Online (Sandbox Code Playgroud)

然后我想使用这个字符串来创建一个XElement实例,如:

XElement element =  XElement.Load(var);
Run Code Online (Sandbox Code Playgroud)

c#

19
推荐指数
4
解决办法
2万
查看次数

如何在C#中添加具有不同前缀/命名空间的xml属性?

我需要能够创建一个如下所示的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#的根元素中为属性设置不同的前缀?

c# xml xml-attribute

6
推荐指数
1
解决办法
3267
查看次数

标签 统计

c# ×2

xml ×1

xml-attribute ×1