Подтвердить что ты не робот

Игнорировать пространства имен в LINQ to XML

Как мне LINQ to XML iqnore все пространства имен? Или изменить, как я выделяю пространства имен?

Я спрашиваю, потому что пространства имен задаются полувариантным образом, и мне надоело искать узлы как с пространством имен, так и без него.

4b9b3361

Ответ 1

Вместо записи:

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

Тоже для атрибутов, если вам приходится часто обращаться с именами атрибутов (что относительно редко).

[EDIT] Добавление решения для XPath

Для XPath вместо записи:

/foo/bar | /foo/ns:bar | /ns:foo/bar | /ns:foo/ns:bar

вы можете использовать функцию local-name():

/*[local-name() = 'foo']/*[local-name() = 'bar']

Ответ 2

Здесь используется способ разбиения пространств имен:

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

Ответ 3

Как я нашел этот вопрос в поисках простого способа игнорировать пространства имен в атрибутах, здесь расширение для игнорирования пространств имен при доступе к атрибуту на основе ответа Павла (для упрощения копирования я включил его расширение):

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