0

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

3
  • 5
    Can you expand upon "doesn't work"? What is it doing? Commented Nov 4, 2015 at 21:47
  • 5
    What doesn't work? Are you getting an exception? Commented Nov 4, 2015 at 21:47
  • 1
    I would recommend looking into the XML Serialization framework in .NET. You can turn your XML into an object and compare the members of the two classes. Starting with VS2013 you can Paste XML into the Editor window and have it generate a class based on the XML structure. Commented Nov 4, 2015 at 21:53

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Or in one line, List<string> list = xdoc.Elements("kategorie").Elements().Select(p => p.Value).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.