Reputation: 7411
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>
Upvotes: 1
Views: 194
Reputation: 94625
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);
Upvotes: 3
Reputation: 273179
Something like
var names = doc.Root.Descendants("dac").Elements("name");
This assumes each <dac>
contains at most 1 <name>
element.
Upvotes: 2