Search XDocument using LINQ without knowing the namespace

后端 未结 6 1809
春和景丽
春和景丽 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 05:13

    As Adam precises in the comment, XName are convertible to a string, but that string requires the namespace when there is one. That's why the comparison of .Name to a string fails, or why you can't pass "Person" as a parameter to the XLinq Method to filter on their name.
    XName consists of a prefix (the Namespace) and a LocalName. The local name is what you want to query on if you are ignoring namespaces.
    Thank you Adam :)

    You can't put the Name of the node as a parameter of the .Descendants() method, but you can query that way :

    var doc= XElement.Parse(
    @"
    
      
        
            83838
            Tom
            Jackson
        
        
            789875
            Chris
            Smith
        
       
       
    ");
    

    EDIT : bad copy/past from my test :)

    var persons = from p in doc.Descendants()
                  where p.Name.LocalName == "Person"
                  select p;
    
    foreach (var p in persons)
    {
        Console.WriteLine(p);
    }
    

    That works for me...

提交回复
热议问题