如何将 xml 命名空间添加到 xml 文档

发布于 2024-09-03 07:54:43 字数 581 浏览 2 评论 0原文

我正在尝试创建一个 xml,应该如下所示

<?xml version="1.0" encoding="iso-8859-1"?>
<MyTestSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Tests>
    <Test>
      <messaure>1</messaure>
      <height>4</height>
    </Test>
    <Test>
      <messaure>4</messaure>
      <height>53</height>
    </Test>
  </Tests>
</MyTestSet>

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

Im trying to create a xml the should look like this

<?xml version="1.0" encoding="iso-8859-1"?>
<MyTestSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Tests>
    <Test>
      <messaure>1</messaure>
      <height>4</height>
    </Test>
    <Test>
      <messaure>4</messaure>
      <height>53</height>
    </Test>
  </Tests>
</MyTestSet>

Its not a problem to create the Tests or Test elements, but what is the best way to Create the "MyTestSet" including the namespaces?
Im using c# XMLDocument

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-09-10 07:54:43

这对我有用:

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);

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

This works for me:

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

If you want to create the entire document you've posted, you might not want to forget the XML declaration:

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

        XmlElement document_element = XmlDocument.DocumentElement;
        XmlDocument.InsertBefore(xml_declaration, document_element);

In certain cases you might need it.

同尘 2024-09-10 07:54:43

这个问题还展示了另一种方法:
在 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

This question also shows another way of doing this:
Creating a specific XML document using namespaces in C#

You could alternatively use the XmlNamespaceManager class

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

Finally there is always Linq too, you could use a XDocument and XNamespace

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

冷清清 2024-09-10 07:54:43
XmlDocument xmlDocSPack = new XmlDocument();
XmlNode xmldocNode = xmlDocSPack.CreateXmlDeclaration("1.0", "", null);
xmlDocSPack.AppendChild(xmldocNode);

XmlElement LiftsMainNode = xmlDocSPack.CreateElement("Lifts");
LiftsMainNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

xmlDocSPack.AppendChild(LiftsMainNode);
xmlDocSPack.Save("SPack"+DateTime.Now.Year + ".xml");

输出 :

<?xml version="1.0"?>
<Lifts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
XmlDocument xmlDocSPack = new XmlDocument();
XmlNode xmldocNode = xmlDocSPack.CreateXmlDeclaration("1.0", "", null);
xmlDocSPack.AppendChild(xmldocNode);

XmlElement LiftsMainNode = xmlDocSPack.CreateElement("Lifts");
LiftsMainNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

xmlDocSPack.AppendChild(LiftsMainNode);
xmlDocSPack.Save("SPack"+DateTime.Now.Year + ".xml");

Output :

<?xml version="1.0"?>
<Lifts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文