如何获得父节点子节点的子节点的值

问题描述 投票:-1回答:1

我知道这个问题似乎很混乱,因为它对我来说很混乱。我认为我最好以身作则。我有一个部分xml文件,如下所示。我需要获取VALUE节点的DIALVALUE子节点的值,但是要获取正确的值,我需要通过DIALVALUE id属性找到正确的DIAL节点。 xml文件中DIALVALUE节点被列出71次,所需的值不正确。如何做到这一点?

<?xml version="1.0" encoding="utf-16"?>
<OrderXml>
    <DialValues>
      <DialValue>
        <Dial id="11144" externalId="">
          <PlanName>pg1_CreditUnionName</PlanName>
          <DisplayName>Credit Union Name:</DisplayName>
        </Dial>
        <Value>Alexis Nab Credit Union</Value>
      </DialValue>
      <DialValue>
        <Dial id="11145" externalId="">
          <PlanName>pg1_CharterNumber</PlanName>
          <DisplayName>Charter Number:</DisplayName>
        </Dial>
        <Value>9999</Value>
      </DialValue>
      <DialValue>
        <Dial id="11146" externalId="">
          <PlanName>pg1_ContactNameFirst</PlanName>
          <DisplayName>Solution Main Contact First Name:</DisplayName>
        </Dial>
        <Value>Alexis</Value>
      </DialValue>
      <DialValue>
    </DialValues>
<OrderXml>

c# .net xml linq-to-xml
1个回答
0
投票
var value = XDocument.Parse(xml) .Descendants("Dial") .Single(el => el.Attribute("id").Value == "11145") .Parent.Descendants("Value").Single().Value;
© www.soinside.com 2019 - 2024. All rights reserved.