29

我正在尝试使用 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");

如何将剩余的 3 个声明添加到同一个元素?

4

3 回答 3

49
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获得。

于 2009-12-15T05:07:50.227 回答
8

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/也注册了名称表中的前缀。

于 2018-02-10T10:06:14.300 回答
0

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

于 2009-12-15T04:54:16.847 回答