C# 如何获取父节点子节点的值子节点

C# 如何获取父节点子节点的值子节点,c#,.net,xml,linq-to-xml,C#,.net,Xml,Linq To Xml,我知道这个问题看起来很困惑,因为对我来说这是一个困惑。我想我最好以身作则。我有一个部分xml文件,如下所示。我需要获得DIALVALUE节点的value子节点的值,但要获得正确的值,我需要通过DIALid属性找到正确的DIALVALUE节点。DIALVALUE节点在xml文件中列出了71次,所需的值不符合顺序。如何做到这一点 <?xml version="1.0" encoding="utf-16"?> <OrderXml> <DialValues>

我知道这个问题看起来很困惑,因为对我来说这是一个困惑。我想我最好以身作则。我有一个部分xml文件,如下所示。我需要获得
DIALVALUE
节点的
value
子节点的值,但要获得正确的值,我需要通过
DIAL
id属性找到正确的
DIALVALUE
节点。
DIALVALUE
节点在xml文件中列出了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>


pg1_名称
储蓄互助社名称:
亚历克西斯国民银行信用社
pg1_编号
租船号码:
9999
pg1_ContactNameFirst
解决方案主要联系人姓名:
亚历克西斯

这应该会给你一个想法(注意:包括空处理等)

使用字典:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            reader.ReadLine(); // skip the utf-16 which Net doesn't like
            XDocument doc = XDocument.Load(reader);

            Dictionary<int, XElement> dict = doc.Descendants("DialValue")
                .GroupBy(x => (int)x.Element("Dial").Attribute("id"), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }

}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
StreamReader=新的StreamReader(文件名);
reader.ReadLine();//跳过网络不喜欢的utf-16
XDocument doc=XDocument.Load(读卡器);
Dictionary dict=文档子体(“DialValue”)
.GroupBy(x=>(int)x.Element(“拨号”).Attribute(“id”),y=>y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}
}
}

这是否回答了您的问题?我希望你能这么简单,但是,不,这不能回答我的问题。我需要根据
id获取
节点。我将如何处理空可能性?
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            reader.ReadLine(); // skip the utf-16 which Net doesn't like
            XDocument doc = XDocument.Load(reader);

            Dictionary<int, XElement> dict = doc.Descendants("DialValue")
                .GroupBy(x => (int)x.Element("Dial").Attribute("id"), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }

}