Ignore namespaces in LINQ to XML

前端 未结 3 1635
北荒
北荒 2020-11-28 06:20

How do I have LINQ to XML iqnore all namespaces? Or alteranately, how to I strip out the namespaces?

I\'m asking because the namespaces are being set in a semi-rando

3条回答
  •  没有蜡笔的小新
    2020-11-28 06:29

    As I found this question in search for an easy way to ignore namespaces on attributes, here's an extension for ignoring namespaces when accessing an attribute, based on Pavel´s answer (for easier copying, I included his extension):

    public static XAttribute AttributeAnyNS(this T source, string localName)
    where T : XElement
    {
        return source.Attributes().SingleOrDefault(e => e.Name.LocalName == localName);
    }
    
    public static IEnumerable ElementsAnyNS(this IEnumerable source, string localName)
    where T : XContainer
    {
        return source.Elements().Where(e => e.Name.LocalName == localName);
    }
    

提交回复
热议问题