【问题标题】:reading from a xml and bind to treeview after eliminating the rootnode and firstchild node消除根节点和第一个子节点后,从 xml 读取并绑定到树视图
【发布时间】:2012-07-24 13:41:03
【问题描述】:

我有一个结构的xml文件

<MainList>
<number>5<number/>
<first id="1" name="test" />
<second/>
<third/>
<MainList/>

现在我想通过消除“MainList”和“number”在树视图中显示它

我希望树以格式显示

first
  id
  name
second
third

我现在正在使用 xmlreader

【问题讨论】:

  • 到目前为止您尝试过什么?您必须使用XmlReader 还是可以使用XDocument 代替?
  • 我已经用 xmlreader 列出了 xml。但我只能列出完整的 xml,即,没有消除“MainList”和“number”。
  • 顺便说一句,您的 XML 中有一个错字:&lt;number&gt;5&lt;number/&gt; 应该是 &lt;number&gt;5&lt;/number&gt;&lt;MainList/&gt; 应该是 &lt;/MainList&gt;

标签: c# xml winforms


【解决方案1】:

根据此处@AS-CII (Binding hierarchical xml to treeview) 提供的答案,您可以修改此处发布的代码以满足您的需求。

  1. 跳过根元素和第一个子元素
  2. 将一级-TreeNodes 直接添加到Nodes-TreeView 的集合中(因为没有根元素)
  3. AttributeElement 创建TreeNode

例如:

private void Form1_Load ( object sender, EventArgs e )
{
    string xml = "<MainList><number>5</number><first id=\"1\" name=\"test\" /><second/><third/></MainList>";
    XDocument doc = XDocument.Parse ( xml );
    var elements = doc.Root // "MainList"
        .Elements () // Elements inside "MainList"
        .Skip ( 1 ); // Skip first item ("number")
    // Add a TreeNode for each element on first level (inside MainList)
    foreach ( var item in elements )
    {
        // Create first-level-node
        TreeNode node = new TreeNode ( item.Name.LocalName );
        // Create subtree, if necessary
        TreeNode[] nodes = this.GetNodes ( node, item ).ToArray ();
        // Add node with subtree to TreeView
        treeView1.Nodes.AddRange ( nodes );
    }
}

上面的代码使用以下方法从 XML 文档的元素和属性创建 TreeNodes:

private IEnumerable<TreeNode> GetNodes ( TreeNode node, XElement element )
{
    List<TreeNode> result = new List<TreeNode> ();
    // First, create TreeNodes for attributes of current element and add them to the result
    if ( element.HasAttributes )
        result.AddRange ( node.AddRange (
            from item in element.Attributes ()
            select new TreeNode ( "[A] " + item.Name.LocalName ) ) );
    // Next, create subtree and add it to the result
    if ( element.HasElements )
        result.AddRange ( node.AddRange ( 
            from item in element.Elements ()
            let tree = new TreeNode ( item.Name.LocalName )
            from newNode in GetNodes ( tree, item )
            select newNode ) );
    // If there aren't any attributes or subelements, return the node that was originally passed in
    if ( result.Count == 0 )
        result.Add ( node );
    // Return the result 
    return result;
}

另外,你需要这个扩展方法:

public static class TreeNodeEx
{
    public static IEnumerable<TreeNode> AddRange ( this TreeNode collection, IEnumerable<TreeNode> nodes )
    {
        collection.Nodes.AddRange ( nodes.ToArray () );
        return new[] { collection };
    }
}

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多