C# 如何添加InnerXml而不进行任何修改?
我试图找到一种简单的方法,使用xmlns将XML添加到XML中,而不必每次都指定C# 如何添加InnerXml而不进行任何修改?,c#,.net,xml,xml-namespaces,C#,.net,Xml,Xml Namespaces,我试图找到一种简单的方法,使用xmlns将XML添加到XML中,而不必每次都指定xmlns=“” 我尝试了XDocument和XmlDocument,但找不到简单的方法。我得到的最接近的结果是: XmlDocument xml = new XmlDocument(); XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null); xml.AppendChild(docNode); XmlElement root = xml.
xmlns=“”
我尝试了XDocument
和XmlDocument
,但找不到简单的方法。我得到的最接近的结果是:
XmlDocument xml = new XmlDocument();
XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(docNode);
XmlElement root = xml.CreateElement("root", @"http://example.com");
xml.AppendChild(root);
root.InnerXml = "<a>b</a>";
xmldocumentxml=newxmldocument();
XmlNode docNode=xml.CreateXmlDeclaration(“1.0”,“UTF-8”,null);
AppendChild(docNode);
XmlElement root=xml.CreateElement(“根”,“@”http://example.com");
AppendChild(根);
root.InnerXml=“b”;
但我得到的是:
<root xmlns="http://example.com">
<a xmlns="">b</a>
</root>
B
那么:有没有一种方法可以在不修改的情况下设置
InnerXml
呢?您可以像创建root
元素一样创建XmlElement
,并指定该元素的InnerText
备选案文1:
string ns = @"http://example.com";
XmlDocument xml = new XmlDocument();
XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(docNode);
XmlElement root = xml.CreateElement("root", ns);
xml.AppendChild(root);
XmlElement a = xml.CreateElement("a", ns);
a.InnerText = "b";
root.AppendChild(a);
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com">
<a xmlns="">b</a>
</root>
备选案文2:
XmlDocument xml = new XmlDocument();
XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(docNode);
XmlElement root = xml.CreateElement("root");
xml.AppendChild(root);
root.SetAttribute("xmlns", @"http://example.com");
XmlElement a = xml.CreateElement("a");
a.InnerText = "b";
root.AppendChild(a);
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com">
<a xmlns="http://example.com">b</a>
</root>
结果XML:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com">
<a>b</a>
</root>
备选案文2:
XmlDocument xml = new XmlDocument();
XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
xml.AppendChild(docNode);
XmlElement root = xml.CreateElement("root");
xml.AppendChild(root);
root.SetAttribute("xmlns", @"http://example.com");
XmlElement a = xml.CreateElement("a");
a.InnerText = "b";
root.AppendChild(a);
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com">
<a xmlns="http://example.com">b</a>
</root>
B
xmlns=“”是因为您正在添加来自不同命名空间的元素。将它们添加到父元素的名称空间中,您不应该看到我编写的xmlns=”“part-我尝试不必在每个节点中指定名称空间。如果从父元素获取,则无需硬编码itxml元素的标识,包括它们的名称和名称空间。您所看到的是XmlDocument试图保留您传递的标识,它并没有使您的输入变得更受欢迎。但问题很清楚,所以我不会对此进行争论。正如我所写的-我试图不必在每个节点中指定名称空间。@ispiro不会将名称空间添加到每个节点,但为了避免有多个xmlns
属性,您需要在创建它们时指定它们在同一名称空间中。我知道,为您创建的每个xmlement
反复指定名称空间是相当烦人的,但要获得您想要的XML,这正是您需要做的。我认为有人建议的是一个解决方案。如果执行此操作,则命名空间将仅位于根节点。其在子节点中显示为xmlns=”“
的原因是其位于与子节点不同的命名空间中parent@SamHolder&JG在SD中-我理解这一点。但正如我所写的,我试图不必在每个节点中指定名称空间。看看你的代码。是的!非常感谢你的毅力。您的解决方案非常有效(即使使用root.InnerXml=“b”
)。