XDocument.Descendants没有返回后裔后裔、XDocument、Descendants

2023-09-03 04:42:57 作者:煙╰ゝ刺痛ㄋ眼

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SetNationalList xmlns="http://www.lge.com/ddc">
      <nationalList>
        <portnumber>6000</portnumber>
        <slaveaddress>7000</slaveaddress>
        <flagzone>2</flagzone>
        <flagindivisual>5</flagindivisual>
        <flagdimming>3</flagdimming>
        <flagpattern>6</flagpattern>
        <flaggroup>9</flaggroup>
      </nationalList>
    </SetNationalList>
  </s:Body>
</s:Envelope>

XDocument xdoc = XDocument.Parse(xml);
foreach (XElement element in xdoc.Descendants("nationalList"))
{
   MessageBox.Show(element.ToString());
}

我想在 nationalList 通过每一个节点重复,但它不工作对我来说,它跳过了的foreach 环路完全。我在做什么错在这里?

I'd like to iterate through every nodes under nationalList but it isn't working for me, it skips the foreach loop entirely. What am I doing wrong here?

推荐答案

你不包括命名空间,这是http://www.lge.com/ddc,从父元素默认:

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")
...