开发者

How to retrieve the node elements based on the name given in linq from XML file

开发者 https://www.devze.com 2023-02-15 06:52 出处:网络
My sample xml: <Documents> <Doc> <Name>CSC Customer</Name> <Path>~/Downloads/DocumentTemplates/ACCT_CSC_Customer NSF Check Letter Template.htm</Path>

My sample xml:

<Documents>
    <Doc>
        <Name>CSC Customer</Name>
        <Path>~/Downloads/DocumentTemplates/ACCT_CSC_Customer NSF Check Letter Template.htm</Path>
    </Doc>
    <Doc>
        <Name>VPS Violation NSF</Name>
        <Path>~/Downloads/DocumentTemplates/ACCT_VPS_Violation NSF Check Letter Template.htm</Path>
    </Doc>
</Documents>

This is the sample xml file and i want to get the path based on the node "Name" given.

for example if i give "CSC Cusotmer" i开发者_如何学Python have to get the corresponding path. i want the solution through the Linq.

How do i get the values ( path ) from the xml file using linq


Assuming your XML is in a file called "data.xml" this should work:

var root = XElement.Load(@"data.xml");
var path = (from e in root.Elements("Doc")
            where e.Element("Name").Value == "CSC Customer"
            select e.Element("Path").Value).FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消