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?我能阻止它吗?
基础教程推荐
- 从 C# 控制相机设备 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
