本文介绍了为什么 .NET XML 将 xlmns 属性附加到我添加到文档的 XmlElements?我能阻止它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 XmlElement 添加到现有文档,但正在添加一个额外的属性.这是代码:

I am adding XmlElement to an existing document but an extra attribute is being added. Here is the code:

XmlNode manifest = this.getManifestNode ();
XmlElement manifestEntry = _content.CreateElement ("item", _contentPath);
XmlAttribute id = _content.CreateAttribute ("id");
id.Value = "content" + getManifestNodes ().Count;
XmlAttribute href = _content.CreateAttribute ("href");
href.Value = splitPath [splitPath.Length - 1];
XmlAttribute mediaType = _content.CreateAttribute ("media-type");
mediaType.Value = "application/xhtml+xml";
manifestEntry.Attributes.Append (id);
manifestEntry.Attributes.Append (href);
manifestEntry.Attributes.Append (mediaType);
manifest.AppendChild (manifestEntry);

以及生成的 XML:

<item id="content3" href="test1.html" media-type="application/xhtml+xml" xmlns="/home/jdphenix/epubtest/test/OEBPS/content.opf" />

在哪里

xmlns="/home/jdphenix/epubtest/test/OEBPS/content.opf"

来自?它添加的路径是文档在磁盘上的位置,但我没有在我的代码中添加它(至少,我知道).如果您需要了解更多详细信息,请告诉我.

coming from? The path that it adds is the location of the document on disk, but I'm not adding it in my code (atleast, that I am aware of). Let me know if you need to know more details.

我根据 Filburt 的建议修改了我的代码并更改了

I modified my code per Filburt's suggestion and changed

XmlElement manifestEntry = _content.CreateElement ("item", _contentPath);

XmlElement manifestEntry = _content.CreateElement ("item");

这是朝着正确方向迈出的一步,但会生成以下 XML:

This is a step in the right direction, but produces the following XML:

<item id="content3" href="test1.html" media-type="application/xhtml+xml" xmlns="" />

推荐答案

你自己添加了这个命名空间(第 2 行):

You're adding this namespace yourself (Line 2):

XmlElement manifestEntry = _content.CreateElement ("item", _contentPath);

请参阅 XmlDocument.CreateElement Method (String, String) -第一个字符串参数是您要添加的元素的限定名称,第二个字符串是命名空间.

See XmlDocument.CreateElement Method (String, String) - the first String parameter is the qualified name of the element you are adding and the second string is the namespace.

试试

XmlElement manifestEntry = _content.CreateElement ("item");

一切都会好起来的.

这篇关于为什么 .NET XML 将 xlmns 属性附加到我添加到文档的 XmlElements?我能阻止它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:20