C# 不带命名空间的子元素的XNode到字符串
当我使用xpath和doC# 不带命名空间的子元素的XNode到字符串,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,当我使用xpath和do.ToString()获取子元素时,它会将父名称空间添加到其中。如何在没有冗余名称空间的情况下获取内容?您可以使用此扩展方法。它将递归地创建另一个XElement,包括它的子元素(不带命名空间)。扩展方法需要放在静态类中: <div xmlns="http://www.com"> <div class="child"> </div> </div> 公共静态XElement IgnoreNamespace(此XEle
.ToString()
获取子元素时,它会将父名称空间添加到其中。如何在没有冗余名称空间的情况下获取内容?您可以使用此扩展方法。它将递归地创建另一个XElement,包括它的子元素(不带命名空间)。扩展方法需要放在静态类中:
<div xmlns="http://www.com">
<div class="child">
</div>
</div>
公共静态XElement IgnoreNamespace(此XElement xelem)
{
xxmlns=“”;
var name=xmlns+xelem.name.LocalName;
返回新的XElement(名称,
来自xelem.Elements()中的e
选择e.IgnoreNamespace(),
xelem.Attributes()
);
}
静态void Main(字符串[]参数)
{
var document=XDocument.Parse(“”);
var first=document.Root.Elements().first().IgnoreNamespace();
Console.WriteLine(first.ToString());
}
Raman的答案很好,但它不适用于这样的XML:
public static XElement IgnoreNamespace(this XElement xelem)
{
XNamespace xmlns = "";
var name = xmlns + xelem.Name.LocalName;
return new XElement(name,
from e in xelem.Elements()
select e.IgnoreNamespace(),
xelem.Attributes()
);
}
static void Main(string[] args)
{
var document = XDocument.Parse("<?xml version=\"1.0\" ?><div xmlns=\"http://www.ya.com\"><div class=\"child\"></div></div>");
var first = document.Root.Elements().First().IgnoreNamespace();
Console.WriteLine(first.ToString());
}
这里提供的答案都不适合我。对于这里的其他人,请使用我自己的大型xaml文件和代码片段中包含的示例进行测试
public static XElement IgnoreNamespace(this XElement xelem)
{
XNamespace xmlns = "";
var name = xmlns + xelem.Name.LocalName;
var result = new XElement(name,
from e in xelem.Elements()
select e.IgnoreNamespace(),
xelem.Attributes()
);
if (!xelem.HasElements)
result.Value = xelem.Value;
return result;
}
很难判断此解决方案是否有效,根据您提供的输入,它会抛出
XmlException
:前缀“”无法从“”重新定义为“http://www.ya.com'在同一开始元素标记中。
证明此解决方案不起作用,因为传递不带命名空间的名称
,然后复制属性(包括名称空间声明)是无效的。
public static XElement IgnoreNamespace(this XElement xelem)
{
XNamespace xmlns = "";
var name = xmlns + xelem.Name.LocalName;
var result = new XElement(name,
from e in xelem.Elements()
select e.IgnoreNamespace(),
xelem.Attributes()
);
if (!xelem.HasElements)
result.Value = xelem.Value;
return result;
}
void Main()
{
var path = @"C:\Users\User\AppData\Local\Temp\sample.xml";
var text = File.ReadAllText(path);
var surrounded = "<test xmlns=\"http://www.ya.com\">" + text + "</test>";
var xe = XElement.Parse(surrounded);
xe.Dump();
xe.StripNamespaces().Dump();
var sampleText = "<test xmlns=\"http://www.ya.com\"><div class=\"child\" xmlns:diag=\"http://test.com\"> ** this is some text **</div></test>";
xe = XElement.Parse(sampleText);
xe.Dump();
xe.StripNamespaces().Dump();
}
public static class Extensions
{
public static XNode StripNamespaces(this XNode n)
{
var xe = n as XElement;
if(xe == null)
return n;
var contents =
// add in all attributes there were on the original
xe.Attributes()
// eliminate the default namespace declaration
.Where(xa => xa.Name.LocalName != "xmlns")
.Cast<object>()
// add in all other element children (nodes and elements, not just elements)
.Concat(xe.Nodes().Select(node => node.StripNamespaces()).Cast<object>()).ToArray();
var result = new XElement(XNamespace.None + xe.Name.LocalName, contents);
return result;
}
#if !LINQPAD
public static T Dump<T>(this T t, string description = null)
{
if(description != null)
Console.WriteLine(description);
Console.WriteLine(t);
return t;
}
#endif
}
let stripNamespaces (e:XElement):XElement=
// if the node is not XElement, pass through
let rec stripNamespaces (n:XNode): XNode =
match n with
| :? XElement as x ->
let contents =
x.Attributes()
// strips default namespace, but not other declared namespaces
|> Seq.filter(fun xa -> xa.Name.LocalName <> "xmlns")
|> Seq.cast<obj>
|> List.ofSeq
|> (@) (
x.Nodes()
|> Seq.map stripNamespaces
|> Seq.cast<obj>
|> List.ofSeq
)
XElement(XNamespace.None + x.Name.LocalName, contents |> Array.ofList) :> XNode
| x -> x
stripNamespaces e :?> XElement