在 XmlWriter 中添加多个命名空间声明

发布于 2024-08-14 12:25:14 字数 417 浏览 2 评论 0原文

我正在尝试使用 XmlWriter 写出以下元素

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">

我已经使用

writer.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");

How can I add the left 3 statements to the same element?完成了第一个声明

I am trying to write out the following element using XmlWriter

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">

I've got the very first declaration done using

writer.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");

How can I add the remaining 3 declarations to the same element?

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

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

发布评论

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

评论(3

不羁少年 2024-08-21 12:25:14
writer.WriteAttributeString("xmlns","gx", null, "http://www.google.com/kml/ext/2.2");
writer.WriteAttributeString("xmlns","kml", null, "http://www.opengis.net/kml/2.2");
writer.WriteAttributeString("xmlns","atom", null, "http://www.w3.org/2005/Atom");

https://msdn.microsoft.com 获取/en-us/library/cfche0ka(v=vs.100).aspx

writer.WriteAttributeString("xmlns","gx", null, "http://www.google.com/kml/ext/2.2");
writer.WriteAttributeString("xmlns","kml", null, "http://www.opengis.net/kml/2.2");
writer.WriteAttributeString("xmlns","atom", null, "http://www.w3.org/2005/Atom");

Got that from https://msdn.microsoft.com/en-us/library/cfche0ka(v=vs.100).aspx.

白龙吟 2024-08-21 12:25:14

Ryan B 的答案不完整,因为 XML 命名空间仅写为属性,但未在名称表中注册,因此,LookupPrefix 将无法获取 XML 命名空间之一的前缀 fi http://www.w3.org/2005/Atom。它将返回 null 而不是 atom

要编写命名空间属性并获取命名空间注册,请使用

writer.WriteAttributeString("atom",
                            "http://www.w3.org/2000/xmlns/",
                            "http://www.w3.org/2005/Atom");

使用命名空间http://www.w3.org/2000/xmlns/还会注册名称表中的前缀。

The answer of Ryan B is incomplete as the XML namespace is only written as attribute but not registered in the name table, so LookupPrefix will fail getting prefix of one of the XML namespaces, f.i. http://www.w3.org/2005/Atom. It will return null instead atom.

To write a namespace attribute and get namespace registered use

writer.WriteAttributeString("atom",
                            "http://www.w3.org/2000/xmlns/",
                            "http://www.w3.org/2005/Atom");

Use of namespace http://www.w3.org/2000/xmlns/registers also the prefix in name table.

谷夏 2024-08-21 12:25:14

命名空间只是属性。使用编写元素属性的标准方法。

The namespaces are simply attributes. Use the standards means of writing attributes for the element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文