Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#,XML:将第二个命名空间移动到根元素_C#_Xml_Xsd_Xml Namespaces - Fatal编程技术网

C#,XML:将第二个命名空间移动到根元素

C#,XML:将第二个命名空间移动到根元素,c#,xml,xsd,xml-namespaces,C#,Xml,Xsd,Xml Namespaces,我正在尝试使用System.XML.XmlDocument类创建一个XML文档 我的文档中有两个名称空间 我的C#代码是什么样子的: XmlDocument xDoc = new XmlDocument(); xDoc.InsertBefore(xDoc.CreateXmlDeclaration("1.0","UTF-8","yes"),xDoc.DocumentElement); XmlElement root = xDoc.CreateElement('ROOT','http://examp

我正在尝试使用System.XML.XmlDocument类创建一个XML文档

我的文档中有两个名称空间

我的C#代码是什么样子的:

XmlDocument xDoc = new XmlDocument();
xDoc.InsertBefore(xDoc.CreateXmlDeclaration("1.0","UTF-8","yes"),xDoc.DocumentElement);
XmlElement root = xDoc.CreateElement('ROOT','http://example.org/ns1');
xDoc.AppendChild(root);
XmlElement child1 = xDoc.CreateElement('CHILD1','http://example.org/ns1');
root.AppendChild(child1);
XmlElement child2 = xDoc.CreateElement('ns2:CHILD2','http://example.com/ns2');
root.AppendChild(child2);
XmlElement child3 = xDoc.CreateElement('ns2:CHILD3','http://example.com/ns2');
root.AppendChild(child3);
期望输出:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<ROOT xmlns="http://example.org/ns1" xmlns:ns2="http://example.com/ns2">
    <CHILD1/>
    <ns2:CHILD2/>
    <ns2:CHILD3/>
</ROOT>

实际产量:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<ROOT xmlns="http://example.org/ns1">
    <CHILD1/>
    <ns2:CHILD2 xmlns:ns2="http://example.com/ns2"/>
    <ns2:CHILD3 xmlns:ns2="http://example.com/ns2"/>
</ROOT>

因为第二个名称空间的元素在我的文档中多次出现,所以我不希望重复声明第二个名称空间,而是在根元素中只声明一次

我怎样才能做到这一点


使用LINQ2XML不是我的选择。

只需将所有所需的名称空间作为属性添加到根元素,如

root.SetAttribute("xmlns:ns2", "http://example.com/ns2");
在末尾添加这一行将几乎完全产生您想要的输出(唯一的区别是
xmlns
属性的顺序,但我认为这在您的情况下并不重要):



出于兴趣,为什么不能使用LINQ to XML?它可能会影响答案的其他方面-例如,如果出于某种奇怪的原因,您被限制使用.NET 2.0。那么
root.SetAttribute(“xmlns:ns2”如何http://example.com/ns2");?我不确定这是否能让你得到你想要的。考虑一下XMLINE类()@ jon Skyet中的各种方法:长话短说,我不是直接在C语言中编程,只使用另一种语言中的库,在这里我不能使用.NET数组,因此不使用LINQ的结构。我使用.NET4。0@MariusUt:这是我尝试的第一件事,但它总是删除元素前面的“ns2:”。但是,如果我将其与每个元素的显式名称空间声明相结合,它就会工作。谢谢:)我想您可能已经理解了什么是LINQ到XML——它只是一个XML API,恰好与LINQ配合使用。。。无论如何,LINQ并不需要数组。谢谢。这实际上是我尝试的第一件事,但当时我没有为每个元素显式声明名称空间,所以它不起作用。这两种尝试的组合是:)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT xmlns:ns2="http://example.com/ns2" xmlns="http://example.org/ns1">
    <CHILD1 />
    <ns2:CHILD2 />
    <ns2:CHILD3 />
</ROOT>