views:

140

answers:

6

Hi,

Suppose there is an xml file like below:

<Instances>
  <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image1.jpg" ImageNumber = "1"/>
  <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image2.jpg" ImageNumber = "2"/>
  <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image3.jpg" ImageNumber = "3"/>
  <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image4.jpg" ImageNumber = "4"/>
  <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image5.jpg" ImageNumber = "5"/>
</Instances>

This xml file is read as a string and passed on to a function. This xml file has information about a particular image file. I want to extract the location of all the image files from this string. So whatever is value of "location" filed i need to collect all those value. What is the best way to achieve this in C#.

Thanks,

+16  A: 

Simplest way: parse it as XML (I'd suggest using LINQ to XML) and then extra the information using the XML API. There's no point in treating it as raw character data yourself.

Sample:

XElement root = XElement.Parse(text);
List<string> images = root.Elements("Bits")
                          .Select(x => (string) x.Attribute("Location"))
                          .ToList();

(That will give a null for any Bits element which didn't contain a Location attribute.)

Jon Skeet
+1 for sample code. @Jay - learn LINQ if you are not already familiar with it. It will change how you think about data and data collections.
SethO
When I am trying to use this command, i get an error saying 'System.Xml.Linq.XElement' does not contain a definition for 'Select'. Am i doing something wrong?
Jay
Ok i am sorry, i got it, i was using "Element" instead of "Elements"
Jay
+3  A: 

Not using a string. If it's XML, then read it as such and query it using the XML LINQ libraries.

David M
+3  A: 

If you are parsing XML use the XML classes in the framework, particularly XElement.

Load your data with

XElement element = XElement.Parse(myString);

Then you can easily manipulate the objects with a well defined API.

Stephan
+1  A: 

I would suggest using Linq to XML. With a simple Linq query you could obtain the Location; not parsing necessary.

Adam Driscoll
A: 

you may use Xpath expression for this

Arseny
+3  A: 

Take care, your structure here is not a valid XML for the XElement.Parse because your elements do not have a name, but only attributes.

A possible correct structure would be:

<Instances>
<Image Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image1.jpg" ImageNumber = "1" />
<Image Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image2.jpg" ImageNumber = "2" />
<Image Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image3.jpg" ImageNumber = "3" />
<Image Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image4.jpg" ImageNumber = "4" />
<Image Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image5.jpg" ImageNumber = "5" />
</Instances>

These would result in folowing C# Code for Parsing - based on Jon Skeet's code from above:

 XElement root = XElement.Parse(text);
 List<string> images = root.Elements("Image")
                           .Select(x => (string) x.Attribute("Location"))
                           .ToList();

HTH :)

spookycoder
Awesome great, it works
Jay