为什么.NET XML将xlmns属性附加到我添加到文档的XmlElements? 我可以阻止它吗?
我正在向现有文档添加XmlElement,但正在添加一个额外的属性。 这是代码:
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:
哪里是
xmlns="/home/jdphenix/epubtest/test/OEBPS/content.opf"
来自(哪里? 它添加的路径是文档在磁盘上的位置,但我没有在我的代码中添加它(至少,我知道)。 如果您需要了解更多详细信息,请与我们联系。
编辑:我根据Filburt的建议修改了我的代码并进行了更改
XmlElement manifestEntry = _content.CreateElement ("item", _contentPath);
至
XmlElement manifestEntry = _content.CreateElement ("item");
这是朝着正确方向迈出的一步,但会产生以下XML:
您自己添加此命名空间(第2行):
XmlElement manifestEntry = _content.CreateElement ("item", _contentPath);
请参见XmlDocument.CreateElement Method(String,String) – 第一个String参数是要添加的元素的限定名称,第二个字符串是命名空间。
尝试
XmlElement manifestEntry = _content.CreateElement ("item");
一切都应该没问题。