I am trying to read some data from my XML file
Names/Name/trckid
-
1) For example from an xml file like the one pasted below I want to
read Names/Name/trckid
eg :trckid= FF7eb01f7-7985-47b1-8f8c-a0e92395d00f -
2) as well as Names/Name/Files/file/uri where formatCode=”2001″
eg resulting string: uri=http://cont.catalog.address.xyz.com/e2/ds/9b660964.jpg
Here is my sample XML file
<?xml version="1.0" encoding="utf-8"?>
<Names tracking="trckids" execTime="0" xmlns="urn:schemas-microsoft-com:address:catalog">
<Name xmlns="urn:schemas-microsoft-com:address:catalog" publishState="Published" lcid="1033" options="0" version="65" filterFlags="32687">
<trckid ref="5ofol35" seoMetaData="/xyz/gpscatalog/">FF7eb01f7-7985-47b1-8f8c-a0e92395d00f</trckid>
<providerId>Test0_2286326_304163829</providerId>
<files>
<file formatCode="2001" gpsFileId="9B660964-1626-466C-8359-F036506D8769" height="102" width="136" fileHash="4a4bcceb08b4ccdd16a958e8687c9588">
<uri>http://cont.catalog.address.xyz.com/e2/ds/9b660964-1626-466c-8359-f036506d8769.jpg</uri>
</file>
<file formatCode="2007">
<uri>http://img1.catalog.address.xyz.com/image.aspx?uuid=d7eb01f7-7985-47b1-8f8c-a0e92395d00f&w=136&h=102</uri>
</file>
<file formatCode="2200" gpsFileId="6C34F723-D798-4DA8-AEE4-0442C239D3A0" fileSize="311469" height="180" width="320" sourceFileHash="6ea74770f622e3e0dc9cf5e229c7de7f">
<uri>http://cont.catalog.address.xyz.com/e2/ds/0909a015-4b6a-4574-be63-c2facd527b9f_0.xml</uri>
</file>
</files>
</Name>
<Name xmlns="urn:schemas-microsoft-com:address:catalog" publishState="Published" lcid="1033" options="0" version="65" filterFlags="32687">
<trckid ref="5ofol20" seoMetaData="/xyz/gpscatalog/">FF9eb01f7-7985-47b1-8f8c-a0e92395d00f</trckid>
<providerId>Test1TheLorax_2286326_304163829</providerId>
<files>
<file formatCode="2001" gpsFileId="9B660964-1626-466C-8359-F036506D8769" height="102" width="136" fileHash="4a4bcceb08b4ccdd16a958e8687c9588">
<uri>http://cont.catalog.address.xyz.com/e2/ds/9b660964-1626-466c-8359-f036506d8769.jpg</uri>
</file>
<file formatCode="2007">
<uri>http://img1.catalog.address.xyz.com/image.aspx?uuid=d7eb01f7-7985-47b1-8f8c-a0e92395d00f&w=136&h=102</uri>
</file>
<file formatCode="2200" gpsFileId="6C34F723-D798-4DA8-AEE4-0442C239D3A0" fileSize="311469" height="180" width="320" sourceFileHash="6ea74770f622e3e0dc9cf5e229c7de7f">
<uri>http://cont.catalog.address.xyz.com/e2/ds/0909a015-4b6a-4574-be63-c2facd527b9f_0.xml</uri>
</file>
</files>
</Name>
</Names>
I tried with the below given piece of code
XDocument xdoc = XDocument.Parse(xmlstring);
XNamespace ns = xdoc.Root.GetDefaultNamespace(); // "urn:schemas-microsoft-com:msnvideo:catalog";
IEnumerable<XElement> names = xdoc.Descendants(ns + "name");
foreach (XElement name in names)
{
rssFeedItems.Add(new Adressses
{
trackid= Convert.ToString(name.Element(ns + "trackid").Value),
providerID= Convert.ToString(name.Element(ns + "provideid").Value),
imageUri = Convert.ToString(name.Element(ns + "uri").Value)
});
}
Here am able to read first two items ,ie, trackid and providerID ,but not the image uri
I need to read the uri that matches Names/Name/files/file@formatcode[2001]/uri
something like
imageUri = Convert.ToString(name.Element(ns + "Names/Name/files/file@formatcode[2001]/uri").Value)
but this just doesn’t work . please help
First thing, your xml is invalid. The end tag should look like this
</Names>instead of<Names/>Secondly, you’ve got to add the namespace declaration. Like this:
Also of note, careful with how you append (s) on Element/Elements to make sure you are looking for one or many elements.
Here’s how to iterate over your element collections:
Notice when you debug this that only one of the “Name” elements appears in the collection. This is because the
xmlnsdeclaration doesn’t match for each.