为什么.NET XML追加xlmns属性来XmlElements我添加到文档?我可以阻止它? [英] Why does .NET XML append an xlmns attribute to XmlElements I add to a document? Can I stop it?

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

问题描述

我添加的XmlElement到现有的文档,但额外的属性被添加。这里是code:

  XmlNode的清单= this.getManifestNode();
的XmlElement manifestEntry = _content.CreateElement(项目,_contentPath);
XmlAttribute ID = _content.CreateAttribute(ID);
id.Value =内容+ getManifestNodes().Count之间;
XmlAttribute HREF = _content.CreateAttribute(HREF);
href.Value = splitPath [splitPath.Length  -  1];
XmlAttribute mediaType的= _content.CreateAttribute(媒体类型);
mediaType.Value =应用程序/ xhtml + xml;
manifestEntry.Attributes.Append(ID);
manifestEntry.Attributes.Append(HREF);
manifestEntry.Attributes.Append(mediaType的);
manifest.AppendChild(manifestEntry);
 

和生成的XML:

 <项目ID =content3的href =test1.html媒体类型=是application / xhtml + XML的xmlns =/家/ jdphenix / epubtest /测试/ OEBPS / content.opf/>
 

在哪里

 的xmlns =/家/ jdphenix / epubtest /测试/ OEBPS / content.opf
 

来自何处?它添加的路径是在磁盘上文件的位置,但我没有将它添加在我的code(ATLEAST,我所知道的)。让我知道如果您需要了解更多的细节。

编辑:我修改了code每Filburt的建议和修改

 的XmlElement manifestEntry = _content.CreateElement(项目,_contentPath);
 

 的XmlElement manifestEntry = _content.CreateElement(项目);
 

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

 <项目ID =content3的href =test1.html媒体类型=是application / xhtml + XML的xmlns =/>
 

解决方案

您要添加这个命名空间自己(2号线):

 的XmlElement manifestEntry = _content.CreateElement(项目,_contentPath);
 

请参阅 XmlDocument.CreateElement方法(字符串,字符串) - 中第一个字符串参数是要添加,而第二字符串是命名空间的元素的限定名。

尝试

 的XmlElement manifestEntry = _content.CreateElement(项目);
 

和一切都应该罚款。

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);

and the resulting XML:

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

Where is the

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.

Edit: I modified my code per Filburt's suggestion and changed

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

to

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

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="" />

解决方案

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

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

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.

Try

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

and everything should be fine.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆