简体   繁体   中英

parsing XML using linq to xml

I want to parse some large XML file using linq to XML but I don't know how exactly can I do this. I want to read all <name> tag that come after <dac> tag. I must mentioned I have many <name> tag in XMl file I just want to read that one come after <dac> . How can I do this using linq to xml?

Here is a simple sample of my XML file:
I want the code return me such value: (SSS, HHH, HHH Panel)

<?xml version="1.0" encoding="UTF-8"?>

<installationTree>
  <folder>
    <id>1</id>
    <name>Company</name>
    <full-path>Door List / Company</full-path>
    <nodes>
      <controller>
        <id>9</id>
        <name>9016 Panel</name>
        <description/>        
        <full-path>Door List / Company / 9016 Panel</full-path>
        <nodes>
          <dac>
            <isOneDoorController>false</isOneDoorController>
            <id>9</id>
            <name>SSS</name>
            <description/>
            <address>1</address>
            <active>true</active>
            <externalId>ID:9_20111123_121106</externalId>
            <mode>standard</mode>    

            <doorCodes/>
            <full-path>Door List / Company / 9016 Panel / SSS</full-path>
            <nodes/>
          </dac>
          <dac>
            <isOneDoorController>false</isOneDoorController>
            <id>10</id>
            <name>HHH</name>
            <description/>
            <address>2</address>
            <active>true</active>

            <doorCodes/>
            <full-path>Door List / Company / 9016 Panel / HHH</full-path>
            <nodes/>
          </dac>
        </nodes>
      </controller>
      <oneDoorController>
        <id>8</id>
        <name>HHH Panel</name>
        <description/>
        <serialnumber>00:06:8e:30:24:24</serialnumber>
        <timezone>Iran</timezone>
        <active>true</active>
        <docUpdater/>
        <nodes>
          <dac>
            <isOneDoorController>true</isOneDoorController>
            <id>8</id>
            <name>HHH Panel</name>
            <description/>
            <address>1</address>
            <active>false</active>            
            <doorCodes/>
          </dac>
          <full-path>Door List / Company / HHH Panel</full-path>
          <nodes/>
        </nodes>
      </oneDoorController>
    </nodes>
  </folder>
</installationTree>

You need to load the XML document and write the statement suggested by @Henk Holterman .

string file = @"c:\file.xml";
XDocument doc = XDocument.Load(file);
var names= doc.Root.Descendants("dac").Elements("name");

foreach (var t in names)
   Console.WriteLine(t.Value); 

Something like

 var names = doc.Root.Descendants("dac").Elements("name");

This assumes each <dac> contains at most 1 <name> element.

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