How to query an XDocument with LINQ when elements have a colon in their name?

后端 未结 2 2134
-上瘾入骨i
-上瘾入骨i 2021-02-18 16:16

I am trying to use LINQ to XML in an with the XDocument object. How do you query the result element in the example below?


   

        
2条回答
  •  走了就别回头了
    2021-02-18 17:07

    That colon means that the XML is using namespaces. Based on this blogpost someone posted about LINQ, XML, and namespaces, here's a version of your code that you might want to try.:

    static XName serv(string name)
    {
      return XNamespace.Get("") + name;
    }
    
    XDocument doc = XDocument.Parse(xml);
    string value = doc.Descendants(serv("header")).First().Descendants(serv("response")).First().Descendants(serv("result")).First().Value;
    

提交回复
热议问题