Piotr Bagier
Piotr Bagier

Reputation: 21

XML to List<string> C#

My problem is, I want to convert xml to list which I can compare with other list.

Here is code:

XML:

<?xml version="1.0" encoding="utf-8"?>
<body>
  <firstrun>false</firstrun>
  <kategorie>
    <firstrun>true</firstrun>
    <samochod>true</samochod>
    <samochod1111>true</samochod1111>
    <samochod22222>true</samochod22222>
  </kategorie>
  <stylkolor>1</stylkolor>
  <themekolor>1</themekolor>
</body>

and code in which I want to convert xml to list:

public List<string> wczytajListeKategorii()
{
    XElement xdoc = XElement.Load(fileName);

    var list = xdoc.Elements("kategorie");

    List<string> selectedCollection = list.ToList();

    return selectedCollection;
}

Unfotunatelly it doesn't work.

Thank you for help

Upvotes: 0

Views: 517

Answers (1)

CodeNotFound
CodeNotFound

Reputation: 23200

I don't know what you exactly want to do but let met tell you that your code will not compile because of this line List<string> selectedCollection = list.ToList();

list.ToList()returns a collection od XElement type not a collection of string.

If you just want to retrive the value in each node in kategorie element this is what you want to do :

var list = xdoc.Elements("kategorie").Elements().Select(p => p.Value);
List<string> selectedCollection = list.ToList();

Upvotes: 2

Related Questions