XmlWriter 写入空 xmlns
我使用以下代码来初始化 XmlDocument
XmlDocument moDocument = new XmlDocument();
moDocument.AppendChild(moDocument.CreateXmlDeclaration("1.0", "UTF-8", null));
moDocument.AppendChild(moDocument.CreateElement("kml", "http://www.opengis.net/kml/2.2"));
稍后,在此过程中,我使用以下代码向其写入一些值
using (XmlWriter oWriter = oDocument.DocumentElement.CreateNavigator().AppendChild())
{
oWriter.WriteStartElement("Placemark");
//....
oWriter.WriteEndElement();
oWriter.Flush();
}
这最终在保存文档时给出以下 xml
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark xmlns="">
<!-- -->
</Placemark>
</kml>
如何摆脱地标上的空 xmlns元素?
--经过编辑以显示地标书写方式的更改--
如果我将命名空间放入地标写入中,则不会将任何元素添加到文档中。
I'm using the following code to initialise an XmlDocument
XmlDocument moDocument = new XmlDocument();
moDocument.AppendChild(moDocument.CreateXmlDeclaration("1.0", "UTF-8", null));
moDocument.AppendChild(moDocument.CreateElement("kml", "http://www.opengis.net/kml/2.2"));
Later in the process I write some values to it using the following code
using (XmlWriter oWriter = oDocument.DocumentElement.CreateNavigator().AppendChild())
{
oWriter.WriteStartElement("Placemark");
//....
oWriter.WriteEndElement();
oWriter.Flush();
}
This ends up giving me the following xml when I save the document
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark xmlns="">
<!-- -->
</Placemark>
</kml>
How can I get rid of the empty xmlns on the Placemark element?
--EDITED TO SHOW CHANGE TO HOW PLACEMARK WAS BEING WRITTEN--
If I put the namespace in the write of placemark then non of the elements are added to the document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我通过使用以下代码创建文档(文档元素中没有命名空间)
并通过使用以下代码保存它来在保存之前设置命名空间来
解决此问题。这是有效的,因为仅在保存的 xml 文件中需要命名空间。
I have fixed the issue by creating the document with the following code (no namespace in the document element)
And by saving it with the following code to set the namespace before the save
This is valid as the namespce is only required in the saved xml file.
这是一篇旧帖子,但只是为了防止将来出现不良做法;您不应该永远在 XML 文档中声明 xmlns 命名空间,因此这可能是您得到空节点的原因,因为您正在执行 XmlDocument 不应该执行的操作。
来源:http://www.w3.org/TR/REC- xml-名称/#ns-decl
This is an old post, but just to prevent future bad practice; you should never declare the xmlns namespace in an XML document, so this may be the cause why you get empty nodes since you are doing something the XmlDocument is not supposed to do.
Source: http://www.w3.org/TR/REC-xml-names/#ns-decl
以下代码对我有用(来源):
The following code worked for me (source):
oWriter.WriteStartElement("Placemark");
应该可以工作,因为父节点已经具有正确的命名空间。oWriter.WriteStartElement("Placemark");
should work, because the parent node already has the right namespace.你尝试过吗:
Did you try:
否则,您需要
将 Placemark 元素放入 null 命名空间中,这就是序列化 XML 时添加
xmlns=""
属性的原因。You needed
otherwise the Placemark element gets put in the null namespace, which is why the
xmlns=""
attribute is added when you serialize the XML.