Linq to xml get element value

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Linq to Xml get elementat

<div id="x_post_message_133114
<blockquote>Sorry for the long XML sample below. The XML sample below has two records and each record has three similar Nodes (<datafield tag="500" ) What I want to accomplish is get the value of the second Node <datafield tag = â5000â>.
The fallowing code I gets the value for all three nodes but I want something like get elementat(1). Appreciate your help.<br/>
I tried this but it didnât worked MsgBox(xEle.Elements.ElementAt(1).Value) <br/>
<br/>
Dim xd As XDocument = XDocument.Load("C:UsersstopeteDesktopMarc_conv ertxml.xml")<br/>
If xd IsNot Nothing Then<br/>
Dim datafields = From datafield In xd.Descendants("datafield")<br/>
Where (datafield.Attribute("tag").Value = "500")<br/>
Select datafield<br/>
<br/>
For Each xEle In datafields<br/>
<br/>
MsgBox(xEle)<br/>
<br/>
Next<br/>
<br/>
End If<br/>
<br/>
<br/>
XML:<br/>
<?xml version="1.0"?><br/>
<collection><br/>
<record><br/>
<leader>03799cam 2200493Ia 4500</leader> <br/>
<controlfield tag="001 648168072<br/>
<subfield code="a Battle, Stephen L.</subfield><br/>
</controlfield> <br/>
<controlfield tag="003 SIRSI</controlfield><br/>
<controlfield tag="005 20110207150903.0</controlfield><br/>
<controlfield tag="008 100716s2010 caub bm f000 0 eng d</controlfield><br/>
<controlfield tag="007 cr b|||||a||||</controlfield><br/>
<datafield tag="035" ind1=" " ind2=" <br/>
<subfield code="a AD#</subfield><br/>
<subfield code="c AD#</subfield><br/>
<subfield code="d AD#</subfield><br/>
</datafield><br/>
<datafield tag="037" ind1=" " ind2=" <br/>
<subfield code="a hey</subfield><br/>
</datafield><br/>
<datafield tag="040" ind1=" " ind2=" <br/>
<subfield code="a ADA524509</subfield><br/>
<subfield code="b DTI</subfield> <br/>
</datafield><br/>
<datafield tag="043" ind1=" " ind2=" <br/>
<subfield code="a n-us---</subfield><br/>
</datafield><br/>
<datafield tag="049" ind1=" " ind2=" <br/>
<subfield code="a AD#A</subfield><br/>
</datafield><br/>
<datafield tag="100" ind1="1" ind2=" <br/>
<subfield code="a Battle, Stephen L.</subfield><br/>
</datafield><br/>
<datafield tag="245" ind1="1" ind2="0 <br/>
<subfield code="a Lessons in legitimacy</subfield><br/>
<subfield code="h [electronic resource] :</subfield><br/>
<subfield code="b the LTTE end-game of 2007--2009 /</subfield><br/>
<subfield code="c Stephen L. Battle.</subfield><br/>
</datafield><br/>
<datafield tag="260" ind1=" " ind2=" <br/>
<subfield code="a Monterey, California :</subfield><br/>
<subfield code="b Naval Postgraduate School,</subfield><br/>
<subfield code="c 2010.</subfield><br/>
</datafield><br/>
<datafield tag="300" ind1=" " ind2=" <br/>
<subfield code="a xiv, 57 p. :</subfield><br/>
<subfield code="b ill. (1 col map) ;</subfield><br/>
<subfield code="c 28 cm.</subfield><br/>
</datafield><br/>
<datafield tag="500 <br/>
<subfield code="a Thesis Advisor(s): Borer, Douglas A. ; Second Reader: Chatterjee, Anshu.</subfield><br/>
</datafield><br/>
<datafield tag="500 <br/>
<subfield code="a "June 2010."</subfield><br/>
</datafield><br/>
<datafield tag="500 <br/>
<subfield code="a Author(s) subject terms: LTTE, Counter Insurgency, COIN, Sri Lanka, Tamil Eelam, Eelam War IV, SLAF, South Asia.</subfield><br/>
</datafield><br/>
</record><br/>
<record><br/>
<leader>02919cam 2200493Ia 4500</leader><br/>
<controlfield tag="001 648167554</controlfield><br/>
<controlfield tag="003 SIRSI</controlfield><br/>
<controlfield tag="005 20110207150310.0</controlfield><br/>
<controlfield tag="008 100716s2010 caua bm f000 0 eng d</controlfield><br/>
<controlfield tag="007 cr b|||||a||||</controlfield><br/>
<datafield tag="035" ind1=" " ind2=" <br/>
<subfield code="a AD#</subfield><br/>
<subfield code="c AD#</subfield><br/>
<subfield code="d AD#</subfield><br/>
</datafield><br/>
<datafield tag="037" ind1=" " ind2=" <br/>
<subfield code="a hey</subfield><br/>
</datafield><br/>
<datafield tag="040" ind1=" " ind2=" <br/>
<subfield code="a ADA524541</subfield><br/>
<subfield code="b DTI</subfield><br/>
</datafield><br/>
<datafield tag="043" ind1=" " ind2=" <br/>
<subfield code="a n-us---</subfield><br/>
</datafield><br/>
<datafield tag="049" ind1=" " ind2=" <br/>
<subfield code="a AD#A</subfield><br/>
</datafield><br/>
<datafield tag="100" ind1="1" ind2=" <br/>
<subfield code="a Battaglia, Neal F.</subfield><br/>
</datafield><br/>
<datafield tag="245" ind1="1" ind2="0 <br/>
<subfield code="a Utility of satellite LIDAR waveform data in shallow water</subfield><br/>
<subfield code="h [electronic resource] /</subfield><br/>
<subfield code="c Neal F. Battaglia.</subfield><br/>
</datafield><br/>
<datafield tag="260" ind1=" " ind2=" <br/>
<subfield code="a Monterey, California :</subfield><br/>
<subfield code="b Naval Postgraduate School,</subfield><br/>
<subfield code="c 2010.</subfield><br/>
</datafield><br/>
<datafield tag="300" ind1=" " ind2=" <br/>
<subfield code="a xvi, 67 p. :</subfield><br/>
<subfield code="b ill. (some col.) ;</subfield><br/>
<subfield code="c 28 cm.</subfield><br/>
</datafield><br/>
<datafield tag="500 <br/>
<subfield code="a Thesis Advisor(s): Olsen, Richard C. ; Second Reader: Trask, David M.</subfield><br/>
</datafield><br/>
<datafield tag="500 <br/>
<subfield code="a "June 2010."</subfield><br/>
</datafield><br/>
<datafield tag="500â><br/>
<subfield code="a Author(s) subject terms: LIDAR Waveforms, Underwater LIDAR signature, Spaceborne LIDAR.</subfield><br/>
</datafield><br/>
</record><br/>
</collection></blockquote>



View the full article
 
Back
Top