1

I am trying since a couple of hours and dont get the right result.

I have an XML-File that looks like

<?xml version="1.0" encoding="UTF-8"?>
<ReportVariables>
  <Variables section="Owner">
    <Variable standard="1">
      <Name>Firmenname</Name>
      <Type>String</Type>
    </Variable>
    <Variable standard="0">
      <Name>Filiale</Name>
      <Type>String</Type>
    </Variable>
    <Variable standard="1">
      <Name>Vorname</Name>
      <Type>String</Type>
    </Variable>
    <Variable standard="1">
      <Name>Nachname</Name>
      <Type>String</Type>
    </Variable>
    <Variable standard="1">
      <Name>PLZ</Name>
      <Type>Number</Type>
    </Variable>
    <Variable standard="1">
      <Name>Ort</Name>
      <Type>String</Type>
    </Variable>
    <Variable standard="1">
      <Name>Email</Name>
      <Type>String</Type>
    </Variable>
    <Variable standard="1">
      <Name>Telefon</Name>
      <Type>String</Type>
    </Variable>
  </Variables>
  <Variables section="Customer">
    <Variable standard="1">
      <Name>Telefon</Name>
      <Type>String</Type>
    </Variable>
  </Variables>
<ReportVariables>

and im loading it like

XDocument xml = XDocument.Load(xmlFilename);

Now I have a TreeView and want to have some like

[-]Owner
    [x]Firmenname
        String
    [ ]Filiale
        String
    [x]Vorname
        String
    //... More content
[+]Customer

As you can see, first I want to create a Treeview that lists all elements as described above.

Now I tried xmldocument, xdocument and and and... but I cant get the data returned as expacted.

In second place I want the subchilds to be checkboxes selected according to the variable => standard attribute. But this isnt necessary (atm).

Tried something like this:

var nodes = (from n in xml.Descendants("ReportVariables")
                where n.Element("Variables").Attribute("section").Value == "Owner"
                select n.Element("Variables").Descendants().Elements()).ToList();

But obiously this doesnt work either.

So I have several question.

What is the best to read from XML ? (XDocument or XmlDocument)

Which one is reccommended for my case?

And what is the best way to read from xml to add it to a treeview like described above?

7
  • Use a recursive method. See stackoverflow.com/questions/28976601/… Commented Jun 30, 2017 at 19:57
  • Nah, Charls gave me a very shot and nice solution. Recursive is a bit much code for a small task ^^ but thx for pointing to this. Commented Jun 30, 2017 at 19:59
  • I didn't know how may levels of tags yu wanted in the treeview. Your xml is easy to parse into a dictionary rather than create a custom class. Commented Jun 30, 2017 at 20:04
  • I posted the xml so the taglevel should be clear. :) Anyways, guess there many ways leading to rome. Commented Jun 30, 2017 at 20:06
  • And one leading to the dungeons of the Coliseum. Commented Jun 30, 2017 at 20:09

1 Answer 1

1

I'd probably start by creating a class structure that contains the data you're interested in. Something like:

public class Section
{
    public string Name { get; set; }    
    public List<Variable> Variables { get; set; }
}

public class Variable
{
    public string Name { get; set; }    
    public string Type { get; set; }    
    public bool IsStandard { get; set; }
}

And then query the XML like so:

var sections =
    from section in doc.Descendants("Variables")
    select new Section
    {
        Name = (string) section.Attribute("section"),
        Variables = section
            .Elements("Variable")
            .Select(var => new Variable
            {
                Name = (string) var.Element("Name"),
                Type = (string) var.Element("Type"),
                IsStandard = (int) var.Attribute("standard") == 1
            })
            .ToList()
    };

You can then take that and build your tree view. See this fiddle for a demo.

Sign up to request clarification or add additional context in comments.

1 Comment

This helps me a lot. Thank you for this.

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.