Search XDocument using LINQ without knowing the namespace

后端 未结 6 1807
春和景丽
春和景丽 2020-11-30 04:49

Is there a way to search an XDocument without knowing the namespace? I have a process that logs all SOAP requests and encrypts the sensitive data. I want to find any elemen

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 04:53

    There's a couple answers with extension methods that have been deleted. Not sure why. Here's my version that works for my needs.

    public static class XElementExtensions
    {
        public static XElement ElementByLocalName(this XElement element, string localName)
        {
            return element.Descendants().FirstOrDefault(e => e.Name.LocalName == localName && !e.IsEmpty);
        }
    }
    

    The IsEmpty is to filter out nodes with x:nil="true"

    There may be additional subtleties - so use with caution.

提交回复
热议问题