如何将 xml 命名空间添加到 xml 文档
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
如果您想创建已发布的整个文档,您可能不想忘记 XML 声明:
在某些情况下您可能需要它。
This works for me:
If you want to create the entire document you've posted, you might not want to forget the XML declaration:
In certain cases you might need it.
这个问题还展示了另一种方法:
在 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
输出 :
Output :