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?

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • Use a recursive method. See https://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Jun 30 '17 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. – Dwza Jun 30 '17 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. – jdweng Jun 30 '17 at 20:04
  • I posted the xml so the taglevel should be clear. :) Anyways, guess there many ways leading to rome. – Dwza Jun 30 '17 at 20:06
  • And one leading to the dungeons of the Coliseum. – jdweng Jun 30 '17 at 20:09
  • what would be the recursive method :D – Dwza Jun 30 '17 at 20:24
  • The daily killing of the Beasts. – jdweng Jul 01 '17 at 04:50

1 Answers1

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.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45