Parsing a string in C #

Suppose there is an xml file as shown 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 to the function. This xml file contains information about a specific image file. I want to extract the location of all image files from this line. So, whatever the value of "location" is, I need to collect all these values. What is the best way to achieve this in C #.

Thanks,

+4
source share
6 answers

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

Possible correct structure:

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

This will cause C # Code for Parsing to be based on Jon Skeet code on top:

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

Hth :)

+5
source

The easiest way is to parse it as XML (I would suggest using LINQ to XML) and then additional information using the XML API. It makes no sense to consider it as raw personal data.

Example:

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

(This will give zero for any Bits that does not contain the Location attribute.)

+17
source

Do not use string. If it is XML, then read it as such and query it using the LINQ XML libraries.

+3
source

If you are parsing XML, use XML classes in the structure, in particular XElement.

Upload your data with

 XElement element = XElement.Parse(myString); 

Then you can easily manipulate objects using a well-defined API.

+3
source

I would suggest using Linq to XML . With a simple Linq query, you can get the Location; no need to understand.

+1
source

you can use the xpath expression for this

0
source

Source: https://habr.com/ru/post/1311213/


All Articles