简体   繁体   中英

How to filter the values from xml file and print it to text file in C#

<MasterCode>
    <Uid>119</Uid>
    <Name>Juan Gabriel MONTE</Name>
    <Category>LastName</Category>
  </MasterCode>
<MasterCode>
    <Uid>120</Uid>
    <Name>Atilla SELEK</Name>
    <Category>FirstName</Category>
  </MasterCode>
 <MasterCode>
    <Uid>121</Uid>
    <Name>barbosa filisa</Name>
    <Category>LastName</Category>
  </MasterCode>

This is an example of my xml file. i want to filter only the names that comes under the category last name. how to filter it and print the names in text file with C#?. Please help Thank u in advance.

You can try with XDocument and Linq

var xmlDoc = XDocument.Load("XMLFile2.xml");

var result = xmlDoc.Descendants("MasterCode")
    .Where(element => (string)element.Element("Category") == "LastName")
    .Select(x => x.Element("Name").Value)
    .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM