XmlWriter 写入空 xmlns

发布于 2024-09-09 04:55:27 字数 860 浏览 6 评论 0原文

我使用以下代码来初始化 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 技术交流群。

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

发布评论

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

评论(6

雪若未夕 2024-09-16 04:55:27

我通过使用以下代码创建文档(文档元素中没有命名空间)

XmlDocument moDocument = new XmlDocument(); 
moDocument.AppendChild(moDocument.CreateXmlDeclaration("1.0", "UTF-8", null)); 
moDocument.AppendChild(moDocument.CreateElement("kml"));

并通过使用以下代码保存它来在保存之前设置命名空间来

moDocument.DocumentElement.SetAttribute("xmlns", msNamespace);
moDocument.Save(msFilePath);

解决此问题。这是有效的,因为仅在保存的 xml 文件中需要命名空间。

I have fixed the issue by creating the document with the following code (no namespace in the document element)

XmlDocument moDocument = new XmlDocument(); 
moDocument.AppendChild(moDocument.CreateXmlDeclaration("1.0", "UTF-8", null)); 
moDocument.AppendChild(moDocument.CreateElement("kml"));

And by saving it with the following code to set the namespace before the save

moDocument.DocumentElement.SetAttribute("xmlns", msNamespace);
moDocument.Save(msFilePath);

This is valid as the namespce is only required in the saved xml file.

甜警司 2024-09-16 04:55:27

这是一篇旧帖子,但只是为了防止将来出现不良做法;您不应该永远在 XML 文档中声明 xmlns 命名空间,因此这可能是您得到空节点的原因,因为您正在执行 XmlDocument 不应该执行的操作。

前缀 xmlns 仅用于声明名称空间绑定,并且由
绑定到命名空间名称的定义 http://www.w3.org/2000/xmlns/
不得声明它。其他前缀不得绑定到此
命名空间名称,并且不得将其声明为默认命名空间。
元素名称不得具有前缀 xmlns。

来源: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.

The prefix xmlns is used only to declare namespace bindings and is by
definition bound to the namespace name http://www.w3.org/2000/xmlns/.
It MUST NOT be declared . Other prefixes MUST NOT be bound to this
namespace name, and it MUST NOT be declared as the default namespace.
Element names MUST NOT have the prefix xmlns.

Source: http://www.w3.org/TR/REC-xml-names/#ns-decl

我是男神闪亮亮 2024-09-16 04:55:27

以下代码对我有用(来源):

XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
s.Serialize(xmlWriter, objectToSerialize, ns);

The following code worked for me (source):

XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
s.Serialize(xmlWriter, objectToSerialize, ns);
金橙橙 2024-09-16 04:55:27

oWriter.WriteStartElement("Placemark"); 应该可以工作,因为父节点已经具有正确的命名空间。

oWriter.WriteStartElement("Placemark"); should work, because the parent node already has the right namespace.

少跟Wǒ拽 2024-09-16 04:55:27

你尝试过吗:

oWriter.WriteStartElement("kml", "Placemark", "kml");

Did you try:

oWriter.WriteStartElement("kml", "Placemark", "kml");
横笛休吹塞上声 2024-09-16 04:55:27

否则,您需要

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

将 Placemark 元素放入 null 命名空间中,这就是序列化 XML 时添加 xmlns="" 属性的原因。

You needed

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

otherwise the Placemark element gets put in the null namespace, which is why the xmlns="" attribute is added when you serialize the XML.

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