Search XDocument using LINQ without knowing the namespace

后端 未结 6 1821
春和景丽
春和景丽 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:09

    If your XML documents always defines the namespace in the same node (Request node in the two examples given), you can determine it by making a query and seeing what namespace the result has:

    XDocument xDoc = XDocument.Load("filename.xml");
    //Initial query to get namespace:
    var reqNodes = from el in xDoc.Root.Descendants()
                   where el.Name.LocalName == "Request"
                   select el;
    foreach(var reqNode in reqNodes)
    {
        XNamespace xns = reqNode.Name.Namespace;
        //Queries making use of namespace:
        var person = from el in reqNode.Elements(xns + "Person")
                     select el;
    }
    

提交回复
热议问题