如何获取父节点子节点的子节点的值
How to get value of child node of parent nodes child node
我知道这个问题看起来很混乱,因为它对我来说很混乱。我认为我最好举个例子。我有一个部分 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>
这应该会给您思路(注意:包括空值处理等)。
var value = XDocument.Parse(xml)
.Descendants("Dial")
.Single(el => el.Attribute("id").Value == "11145")
.Parent
.Descendants("Value")
.Single().Value;
使用字典:
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());
}
}
}
我知道这个问题看起来很混乱,因为它对我来说很混乱。我认为我最好举个例子。我有一个部分 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>
这应该会给您思路(注意:包括空值处理等)。
var value = XDocument.Parse(xml)
.Descendants("Dial")
.Single(el => el.Attribute("id").Value == "11145")
.Parent
.Descendants("Value")
.Single().Value;
使用字典:
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());
}
}
}