如何使用XDocument获取xml节点元素值?

时间:2012-11-27 11:02:42

标签: asp.net xml

这里我列出了一个xml。

<?xml version="1.0" encoding="utf-8" ?>
<data>
    <dataitem>
        qqqqqqq
    </dataitem>
    <templatedata>
        <Year>2001</Year>
    </templatedata>
    <mailmergedata>
        <row>
            <facilityname>ABC Corporation</facilityname>
            <dueamount>200.00</dueamount>
        </row>
        <row>
            <facilityname>XYZ Corporation</facilityname>
            <DueAmount>50.00</DueAmount>
        </row>
    </mailmergedata>
</data>

我想检索节点工具名称的值 有人请帮忙

2 个答案:

答案 0 :(得分:1)

试试这个:

XDocument xmlFile = XDocument.Load("xml file path");
var query = from c in xmlFile.Descendants("facilityname ") select c;

然后

foreach (XElement element in query)
{
    Console.WriteLine(element); // get your value here
}

参考Job Skeet的answer,这可能对您有帮助..

答案 1 :(得分:1)

您还可以使用XPathSelectElements,然后使用XPath进行导航:

xdoc.XPathSelectElements("/data/mailmergedata/row/facilityname")