如何将xmlnamespace添加到xmldocument

我试图创建一个xml应该是这样的

    1 4   4 53    

创建测试或测试元素不是问题,但创建包含命名空间的“MyTestSet”的最佳方法是什么? 我使用c#XMLDocument

这对我有用:

 XmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); XmlDocument.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); 

如果要创建已发布的整个文档,可能不想忘记XML声明:

  XmlDeclaration xml_declaration; xml_declaration = XmlDocument.CreateXmlDeclaration("1.0", "ISO-8859-1", "yes"); XmlElement document_element = XmlDocument.DocumentElement; XmlDocument.InsertBefore(xml_declaration, document_element); 

在某些情况下,您可能需要它。

此问题还显示了另一种方法: 使用C#中的命名空间创建特定的XML文档

您也可以使用XmlNamespaceManager类

http://msdn.microsoft.com/en-us/library/d6730bwt%28VS.80%29.aspx

最后总是有Linq,你可以使用XDocument和XNamespace

http://msdn.microsoft.com/en-us/library/bb387075.aspx