Why does .NET XML append an xlmns attribute to XmlElements I add to a document? Can I stop it?(为什么 .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?我能阻止它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 .NET XML 将 xlmns 属性附加到我添加到文档的 XmlElements?我能阻止它吗?
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
