【问题标题】:XML > LINQ Query > Tree ViewXML > LINQ 查询 > 树视图
【发布时间】:2013-10-26 04:12:53
【问题描述】:

我从 www.openstreetmap.org 下载了一些 XML 数据。我现在希望为此应用程序创建一个数据转换器。我进行了广泛的研究,并使用位于 XDocument 类中的 Load 方法制作了一些工作原型。

当前代码:

// 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;
}

示例 XML 数据:

<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>

但是,我的原型目前只能选择给定元素的单个属性值。然后它遍历这些值并输出它们。我现在想通过保留 XML 文档的层次结构并将其转换为“树视图”来进一步完善我的原型。

我计划使用 TreeNode 类来实现这个功能。但我不确定如何维护 XML 文档的层次结构。

示例层次结构:

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

【问题讨论】:

    标签: c# xml linq linq-to-xml


    【解决方案1】:

    从 xml 元素创建 TreeView 相当简单,您不需要任何 LINQ 查询:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多