Quantcast
Viewing latest article 17
Browse Latest Browse All 12583

Parse XML with LINQ C#

Hello,

With C # .NET, I use LINQ to parse XML, but I'm not completely familiar with LINQ for XML.<o:p></o:p>

I have the following XML code:

<?xml version="1.0" encoding="utf-8" ?><Root><PackageVersion version="1.0"/><Window title="test"><ComboBox label="labelTest1"><item>article1</item><item>article2</item><item>article3</item></ComboBox><ComboBox label="labelTest2"><item>base1</item><item>base2</item></ComboBox></Window>  </Root>

I would like to retrieve in this XML file, several information such as version, title and items.

To get the version, here is my code:

IEnumerable<string> version = from item in xelement.Elements("PackageVersion")
select (string)item.Attribute("version");
Console.WriteLine("Version: {0}", version.First());

To get the title, here is my code:<o:p></o:p>

IEnumerable<string> title = from item in xelement.Elements("Window")
select (string)item.Attribute("title");
Console.WriteLine("Title: {0}", titleFirst());

If you have a better way to write the previous two codes, could you show it? Thank you.

I block on the recovery of each comboBox with its label and its items.<o:p></o:p>

I would like to put in two List <string> each item in string.<o:p></o:p>

<o:p> </o:p>

For example, the listComboBox1 list would contain article1, article2 and article3.<o:p></o:p>

The listComboBox2 list would contain base1 and base2.<o:p></o:p>

I tried with the following code:<o:p></o:p>

IEnumerable<XElement> combos = xelement.Elements("Window").Elements("ComboBox").Elements("item");
foreach (var combo in combos)
{     
   Console.WriteLine(combo.Value);
}

but I collect all the items at once and I do not separate them.

I cannot separate the items from each comboBox into two lists.

I would also like to retrieve the label of each combobox separately.<o:p></o:p>

<o:p> </o:p>

Thank you for your help.<o:p></o:p>



<o:p></o:p>


<o:p></o:p>


Viewing latest article 17
Browse Latest Browse All 12583

Trending Articles