XDocument.Descendants not returning descendants

前端 未结 4 1664
暗喜
暗喜 2020-12-17 07:41



        
相关标签:
4条回答
  • 2020-12-17 08:18

    It is correct that you have to include the namespace, but the samples above do not work unless you put the namespace in curly braces:

    XNameSpace ns = "http://www.lge.com/ddc";
    
    foreach (XElement element in xdoc.Descendants("{" + ns + "}nationalList")
    {
          MessageBox.Show(element.ToString());
    }
    

    Greetings Christian

    0 讨论(0)
  • 2020-12-17 08:23

    You have to use the namespace:

    // do _not_ use   var ns = ... here.
    XNameSpace ns = "http://www.lge.com/ddc";
    
    foreach (XElement element in xdoc.Descendants(ns + "nationalList")
    {
          MessageBox.Show(element.ToString());
    }
    
    0 讨论(0)
  • 2020-12-17 08:27

    If you don't want to have to use the ns prefix in all the selectors you can also remove the namespace upfront when parsing the xml. eg:

    string ns = "http://www.lge.com/ddc";
    XDocument xdoc = XDocument.Parse(xml.Replace(ns, string.Empty));
    
    foreach (XElement element in xdoc.Descendants("nationalList")
    ...
    
    0 讨论(0)
  • 2020-12-17 08:34

    You're not including the namespace, which is "http://www.lge.com/ddc", defaulted from the parent element:

    XNamespace ns = "http://www.lge.com/ddc";
    foreach (XElement element in xdoc.Descendants(ns + "nationalList"))
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题