在LINQ to XML中忽略命名空间

93

如何让LINQ to XML忽略所有命名空间?或者说,如何剥离命名空间?

我问这个问题是因为命名空间被以半随机的方式设置,而我已经厌倦了不得不同时搜索具有和不具有命名空间的节点。


3个回答

146

改为:

nodes.Elements("Foo")

写:

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

如果你经常处理命名空间属性(相对较少见),那么同样适用于属性。

[编辑]为XPath添加解决方案

对于XPath,不要写成:
/foo/bar | /foo/ns:bar | /ns:foo/bar | /ns:foo/ns:bar

您可以使用local-name()函数:
/*[local-name() = 'foo']/*[local-name() = 'bar']

如果您知道要查找的元素具有唯一名称,可以使用以下代码跳过所有中间元素:xDoc.Root.Descendants().Where(e => e.Name.LocalName == "SomeName"); - AaronLS

18

这是一个去除命名空间的方法:

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

4

我在搜索一个简单的方法来忽略属性命名空间时发现了这个问题,这里提供一个基于Pavel答案的扩展,用于在访问属性时忽略命名空间(为了更容易复制,我包含了他的扩展):

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接