tags:

views:

80

answers:

2

I have this xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" manifestId="{b91dc6f6-a837-4c5a-bdc8-77a93107af77}" mandatory="No" xmlns="urn:schemas-microsoft-com:PAG:updater-application-block:v2:manifest">
<files base="http:abc/def" hashComparison="Yes" hashProvider="SHA1Managed">
<file source="123.dll" hash="oZlt8qISQxTNETMTSAfhdzJisj+kgir7oWS64+VMbRRCOXXE" />
<file source="234.dll" hash="39UEf/AIp+pSfH4WbftTPVysryCDJBJd/URmUbANpTiAmGHm" />
<file source="abc.dll" hash="2X5d8WfRtBuzLKNZ8geVfy3AmOKaUD7cSI17PbRyF8ds66Cx"  />
</files>
</manifest>

I want to fetch all the file node out of this XML so that i can get the values of source,hash and transient for my further work..

Please tell me in XLINQ...

A: 

Use the Elements selector:

XDocument d = XDocument.Parse(xml);
XNamespace ns = "urn:schemas-microsoft-com:PAG:updater-application-block:v2:manifest";
var files = d.Elements(ns + "manifest").Elements(ns + "files").Elements(ns + "file");

Note the need for an XNamespace because of the no-prefix xmlns declaration in the manifest tag.

itowlson
+1  A: 

If you do not care about the parent nodes, you could use the Descendants selector to simplify the code:

XNamespace ns = "urn:schemas-microsoft-com:PAG:updater-application-block:v2:manifest";
var files = XDocument.Parse(xml).Descendants(ns + "file").Select(x => new {
   Source = (string)x.Attribute("source"),
   Hash =  (string)x.Attribute("hash")
});

files will now store an IEnumerable of strongly typed (anonymous) objects, which expose the Properties 'Source' and 'Hash'

Manu