.net 忽略LINQ to XML中的名称空间
如何让LINQ到XML iqnore所有名称空间?或者,我如何去掉名称空间 我这样问是因为名称空间是以半随机的方式设置的,我已经厌倦了在有名称空间和没有名称空间的情况下搜索节点。而不是写:.net 忽略LINQ to XML中的名称空间,.net,xml,linq,.net,Xml,Linq,如何让LINQ到XML iqnore所有名称空间?或者,我如何去掉名称空间 我这样问是因为名称空间是以半随机的方式设置的,我已经厌倦了在有名称空间和没有名称空间的情况下搜索节点。而不是写: nodes.Elements("Foo") /foo/bar | /foo/ns:bar | /ns:foo/bar | /ns:foo/ns:bar 写: nodes.Elements().Where(e => e.Name.LocalName == "Foo") 当您对此感到厌倦时,请制定自己
nodes.Elements("Foo")
/foo/bar | /foo/ns:bar | /ns:foo/bar | /ns:foo/ns:bar
写:
nodes.Elements().Where(e => e.Name.LocalName == "Foo")
当您对此感到厌倦时,请制定自己的扩展方法:
public static IEnumerable<XElement> ElementsAnyNS<T>(this IEnumerable<T> source, string localName)
where T : XContainer
{
return source.Elements().Where(e => e.Name.LocalName == localName);
}
您可以使用local-name()
函数:
/*[local-name() = 'foo']/*[local-name() = 'bar']
以下是剥离名称空间的方法:
private static XElement StripNamespaces(XElement rootElement)
{
foreach (var element in rootElement.DescendantsAndSelf())
{
// update element name if a namespace is available
if (element.Name.Namespace != XNamespace.None)
{
element.Name = XNamespace.None.GetName(element.Name.LocalName);
}
// check if the element contains attributes with defined namespaces (ignore xml and empty namespaces)
bool hasDefinedNamespaces = element.Attributes().Any(attribute => attribute.IsNamespaceDeclaration ||
(attribute.Name.Namespace != XNamespace.None && attribute.Name.Namespace != XNamespace.Xml));
if (hasDefinedNamespaces)
{
// ignore attributes with a namespace declaration
// strip namespace from attributes with defined namespaces, ignore xml / empty namespaces
// xml namespace is ignored to retain the space preserve attribute
var attributes = element.Attributes()
.Where(attribute => !attribute.IsNamespaceDeclaration)
.Select(attribute =>
(attribute.Name.Namespace != XNamespace.None && attribute.Name.Namespace != XNamespace.Xml) ?
new XAttribute(XNamespace.None.GetName(attribute.Name.LocalName), attribute.Value) :
attribute
);
// replace with attributes result
element.ReplaceAttributes(attributes);
}
}
return rootElement;
}
用法示例:
XNamespace ns = "http://schemas.domain.com/orders";
XElement xml =
new XElement(ns + "order",
new XElement(ns + "customer", "Foo", new XAttribute("hello", "world")),
new XElement("purchases",
new XElement(ns + "purchase", "Unicycle", new XAttribute("price", "100.00")),
new XElement("purchase", "Bicycle"),
new XElement(ns + "purchase", "Tricycle",
new XAttribute("price", "300.00"),
new XAttribute(XNamespace.Xml.GetName("space"), "preserve")
)
)
);
Console.WriteLine(xml.Element("customer") == null);
Console.WriteLine(xml);
StripNamespaces(xml);
Console.WriteLine(xml);
Console.WriteLine(xml.Element("customer").Attribute("hello").Value);
当我在搜索忽略属性上名称空间的简单方法时发现这个问题时,根据Pavel的回答,这里有一个扩展,用于在访问属性时忽略名称空间(为了便于复制,我包括了他的扩展):
public static XAttribute attributeanys(此T源代码,字符串localName)
其中T:XElement
{
返回source.Attributes().SingleOrDefault(e=>e.Name.LocalName==LocalName);
}
公共静态IEnumerable元素SANYNS(此IEnumerable源,字符串localName)
其中T:XContainer
{
返回source.Elements()。其中(e=>e.Name.LocalName==LocalName);
}
如果知道所需元素的名称是唯一的,则可以跳过所有中间元素:xDoc.Root.subjections()。其中(e=>e.Name.LocalName==“SomeName”)代码>另请参见
public static XAttribute AttributeAnyNS<T>(this T source, string localName)
where T : XElement
{
return source.Attributes().SingleOrDefault(e => e.Name.LocalName == localName);
}
public static IEnumerable<XElement> ElementsAnyNS<T>(this IEnumerable<T> source, string localName)
where T : XContainer
{
return source.Elements().Where(e => e.Name.LocalName == localName);
}