JHarley1
JHarley1

Reputation: 177

XML > LINQ Query > Tree View

I have downloaded some XML data from www.openstreetmap.org. I now wish to create a data converter for this application. I have done extensive research and have somewhat of a working prototype using the Load method located within the XDocument class.

Current Code:

// Load the XML document
var xmlDocument = XDocument.Load(@"C:\demo.xml");

// Query the data and write out a subset of contacts
var xmlDocumentQuery = from c in xmlDocument.Root.Descendants("way")
                       select c.Element("tag").Attribute("k");

int indexVariable = 1;
foreach (string name in xmlDocumentQuery)
{
    Console.WriteLine(indexVariable + " " + name);
    indexVariable = indexVariable + 1;
}

Sample XML Data:

<OSM>
<way id="123610420" visible="true" version="1" changeset="8864750" timestamp="2011-07-29T16:26:39Z" user="BillHall" uid="13013">
  <nd ref="1377763151"/>
  <nd ref="1377763152"/>
  <nd ref="1377763156"/>
  <nd ref="1377763165"/>
  <nd ref="1377763192"/>
  <nd ref="1376440397"/>
  <nd ref="1377763200"/>
  <nd ref="1377763151"/>
  <tag k="landuse" v="grass"/>
 </way>
 <way id="123610421" visible="true" version="3" changeset="8890869" timestamp="2011-08-01T13:30:49Z" user="BillHall" uid="13013">
  <nd ref="1377763173"/>
  <nd ref="1377763217"/>
  <nd ref="1377763170"/>
  <nd ref="1377763137"/>
  <nd ref="1378432544"/>
  <nd ref="1377763154"/>
  <nd ref="1378432543"/>
  <nd ref="1377763147"/>
  <nd ref="1376440420"/>
  <nd ref="1376440265"/>
  <nd ref="1378432542"/>
  <nd ref="1376440320"/>
  <nd ref="1376440262"/>
  <nd ref="1377763143"/>
  <nd ref="1377763195"/>
  <nd ref="1381760571"/>
  <nd ref="1381760570"/>
  <nd ref="1377763219"/>
  <nd ref="1377763173"/>
  <tag k="landuse" v="grass"/>
 </way>
 <way id="123610422" visible="true" version="2" changeset="8869626" timestamp="2011-07-30T07:53:36Z" user="BillHall" uid="13013">
  <nd ref="1377763145"/>
  <nd ref="1377763129"/>
  <nd ref="1377763149"/>
  <nd ref="1377763204"/>
  <nd ref="1376440568"/>
  <nd ref="1376440571"/>
  <nd ref="1377763153"/>
  <nd ref="1378432539"/>
  <nd ref="1377763145"/>
  <tag k="landuse" v="grass"/>
 </way>
</OSM>

However, my prototype is currently only able to select a single attribute value of a given element. It then loops through these values and outputs them. I now want to further my prototype by retaining the hierarchy of the XML document and transforming this into a ‘tree view’.

I plan on using the TreeNode class to implement this functionality. But I am uncertain of how to maintain the hierarchy of the XML document.

Example Hierarchy:

<WAY ID = “#”>
    <ND> 
        <ND REF= “#”>
        …   
    </ND>
    <K = “#”>
    <V = “#”>
</WAY>
…

Upvotes: 0

Views: 629

Answers (1)

Sphinxxx
Sphinxxx

Reputation: 13037

Creating a TreeView from xml elements is fairly easy, and you don't need any LINQ queries:

var tree = new TreeView();
CreateTreeNodes(xmlDocument.Root.Elements(), tree.Nodes);


private void CreateTreeNodes(IEnumerable<XElement> elements,
                             TreeNodeCollection treeLevel)
{
    foreach (var element in elements)
    {
        //Create nodes for each xml element..
        var node = new TreeNode(element.Name.LocalName);
        //..add them to the current "level" in the TreeView..
        treeLevel.Add(node);

        //..and then create (and add) each node's child nodes:
        CreateTreeNodes(element.Elements(), node.Nodes);
    }
}

Upvotes: 2

Related Questions