linq到xml-摆脱空白的xmlns [英] linq to xml - get rid of blank xmlns
问题描述
我正在尝试摆脱xml文件中的空名称空间标签.我见过的所有解决方案都是基于从头开始创建xml.我有从以前的xml构造的各种元素.我正在做的是
I'm trying to get rid of empty namespace tags in my xml file. All of the solutions i've seen are based creating the xml from scratch. I have various xelements constructed from a previous xml. All I'm doing is
XElement InputNodes = XElement.Parse(InputXML);
m_Command = InputNodes.Element("Command");
并在各处添加xmlns =".真是气死我了感谢您的帮助.
and it adding the xmlns = "" everywhere. This is really infuriating. Thanks for any help.
推荐答案
有一个帖子,展示了如何轻松(合理地)解决此问题.在输出XML之前,您需要执行以下代码:
There's a post on MSDN blogs that shows how to get around this (reasonably) easily. Before outputing the XML, you'll want to execute this code:
foreach (XElement e in root.DescendantsAndSelf())
{
if (e.Name.Namespace == string.Empty)
{
e.Name = ns + e.Name.LocalName;
}
}
正如张贴者所提到的,替代方法是在添加元素名称时为每个元素名称添加前缀,但这似乎是更好的解决方案,因为它更加自动化,并且节省了键入的时间.
The alternative, as the poster mentions, is prefixing every element name with the namespace as you add it, but this seems like a nicer solution in that it's more automated and saves a bit of typing.
这篇关于linq到xml-摆脱空白的xmlns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!