C#当另一个是特定值时,获取一个XML元素值

时间:2017-02-24 19:39:23

标签: c# xml

只有当<PropertyID>等于0时,C#中是否有可能获取<IsDisabled>节点的值?

如果没有,我怎样才能解析出IsDisabled值为0的PropertyID?

我一整天都在努力,所以任何帮助都会受到赞赏。

我在下面附上了我的XML示例代码段。我已经将它浓缩得很明显,有许多值为1,许多值为0。

<response>
  <code>200</code>
  <result>
    <PhysicalProperty>
      <Property>
        <PropertyID>325213</PropertyID>
        <MarketingName>XXXXX</MarketingName>
        <Type>Student</Type>
        <IsDisabled>1</IsDisabled>
        <IsFeaturedProperty>0</IsFeaturedProperty>
      </Property>
    </PhysicalProperty>
  </result>
</response>

1 个答案:

答案 0 :(得分:1)

是的,LINQ可以很容易地对XML进行这些类型的查询

var propertyIds = XDocument.Parse(myXmlString)
                           .Descendants("Property")
                           .Where(p => p.Element("IsDisabled").Value == "0")
                           .Select(p => p.Element("PropertyID").Value);