简体   繁体   中英

Parsing a string in c#

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,

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.)

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 :)

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

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.

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

你可以使用Xpath表达式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM