在windows应用程序中读取两个xml并创建树视图控件

本文关键字:xml 两个 创建 控件 视图 应用程序 windows 读取 | 更新日期: 2023-09-27 18:11:51

我需要从两个xml动态创建一个树视图:

来自第一个xml的父节点将是项目A(即节点的文本),子节点将是所有文件夹,直到第二个xml的电子邮件,第二个xml的路径在第一个xml中。

请帮帮我,我真的卡住了?

XML 1:
- <Projects>
- <Project id="PROJ_1">
  <Name>Project A</Name> 
  <emailfile>D:'tree'PEMS-Offline-Application'XMLFiles'PROJ_01_EMAILS.xml</emailfile> 
  </Project>
- <Project id="PROJ_2">
  <Name>Project B</Name> 
  <emailfile>D:'tree'PEMS-Offline-Application'XMLFiles'PROJ_02_EMAILS.xml</emailfile> 
  </Project>
- <Project id="PROJ_3">
  <Name>Project C</Name> 
  <emailfile>D:'tree'PEMS-Offline-Application'XMLFiles'PROJ_03_EMAILS.xml</emailfile> 
  </Project>
  </Projects>
XML 2:

- <root>
- <Project id="PROJ_1">
- <folder id="F1.1">
- <incoming id="incoming">
- <emails>
- <email ID="01.1">
  <subject>God Is Great</subject> 
  <to>a.mundra@adapt.com</to> 
  <cc>a.dhiwan@adapt.com</cc> 
  <emaildate>20/04/2013</emaildate> 
  <filepath>C:'currentProject'PEMS-Offline-Application'XMLFiles'PROJ_01_EMAILS.xml</filepath> 
  </email>
- <email ID="01.2">
  <subject>God</subject> 
  <to>a1.mundra@adapt.com</to> 
  <cc>a2.dhiwan@adapt.com</cc> 
  <emaildate>20/05/2013</emaildate> 
  <filepath>C:'currentProject'PEMS-Offline-Application'XMLFiles'PROJ_01_EMAILS.xml</filepath> 
  </email>
  </emails>
  </incoming>
- <outgoing id="outgoing">
- <emails>
  <email ID="01.1" /> 
  <subject>God Is Great</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  <email ID="01.2" /> 
  <subject>God</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  <email ID="01.2" /> 
  <subject>hi</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  </emails>
  </outgoing>
  </folder>
- <folder id="F1.2">
- <incoming id="incoming">
- <emails>
- <email ID="01.1">
  <subject>God Is Great</subject> 
  <to>a.mundra@adapt.com</to> 
  <cc>a.dhiwan@adapt.com</cc> 
  <emaildate>20/04/2013</emaildate> 
  <filepath>C:'currentProject'PEMS-Offline-Application'XMLFiles'PROJ_01_EMAILS.xml</filepath> 
  </email>
- <email ID="01.2">
  <subject>God</subject> 
  <to>a1.mundra@adapt.com</to> 
  <cc>a2.dhiwan@adapt.com</cc> 
  <emaildate>20/05/2013</emaildate> 
  <filepath>C:'currentProject'PEMS-Offline-Application'XMLFiles'PROJ_01_EMAILS.xml</filepath> 
  </email>
  </emails>
  </incoming>
- <outgoing id="outgoing">
- <emails>
  <email ID="01.1" /> 
  <subject>God Is Great</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  <email ID="01.2" /> 
  <subject>God</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  <email ID="01.2" /> 
  <subject>hi</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  </emails>
  </outgoing>
  </folder>
- <folder id="F1.3">
- <incoming id="incoming">
- <emails>
- <email ID="01.1">
  <subject>God Is Great</subject> 
  <to>a.mundra@adapt.com</to> 
  <cc>a.dhiwan@adapt.com</cc> 
  <emaildate>20/04/2013</emaildate> 
  <filepath>C:'currentProject'PEMS-Offline-Application'XMLFiles'PROJ_01_EMAILS.xml</filepath> 
  </email>
- <email ID="01.2">
  <subject>God</subject> 
  <to>a1.mundra@adapt.com</to> 
  <cc>a2.dhiwan@adapt.com</cc> 
  <emaildate>20/05/2013</emaildate> 
  <filepath>C:'currentProject'PEMS-Offline-Application'XMLFiles'PROJ_01_EMAILS.xml</filepath> 
  </email>
  </emails>
  </incoming>
- <outgoing id="outgoing">
- <emails>
  <email ID="01.1" /> 
  <subject>God Is Great</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  <email ID="01.2" /> 
  <subject>God</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  <email ID="01.2" /> 
  <subject>hi</subject> 
  ' 
  <to /> 
  <cc /> 
  <emaildate /> 
  <filepath /> 
  </emails>
  </outgoing>
  </folder>
  </Project>
  </root>

在windows应用程序中读取两个xml并创建树视图控件

下面是c#实现。因为它在后台工作线程,请删除它,如果你不使用。

    private void ConvertXmlNodeToTreeNode(BackgroundWorker worker, DoWorkEventArgs args, XmlNode xmlNode, TreeNodeCollection treeNodes)
    {
        TreeNode newTreeNode = null;
        String nodeText = null;
        if (worker.CancellationPending == true)
        {
            args.Cancel = true;
            return;
        }
        Invoke((MethodInvoker)delegate
        {
            newTreeNode = treeNodes.Add(xmlNode.Name);
        });
        switch (xmlNode.NodeType)
        {
            case XmlNodeType.ProcessingInstruction:
            case XmlNodeType.XmlDeclaration:
                nodeText = "<?" + xmlNode.Name + " " + xmlNode.Value + "?>";
                break;
            case XmlNodeType.Element:
                nodeText = "<" + xmlNode.Name + ">";
                break;
            case XmlNodeType.Attribute:
                nodeText = "ATTRIBUTE: " + xmlNode.Name;
                break;
            case XmlNodeType.Text:
            case XmlNodeType.CDATA:
                nodeText = xmlNode.Value;
                break;
            case XmlNodeType.Comment:
                nodeText = "<!--" + xmlNode.Value + "-->";
                break;
        }
        if (!String.IsNullOrEmpty(nodeText))
        {
            Invoke((MethodInvoker)delegate
            {
                newTreeNode.Text = nodeText;
            });
        }
        if (xmlNode.Attributes != null)
        {
            foreach (XmlAttribute attribute in xmlNode.Attributes)
            {
                ConvertXmlNodeToTreeNode(worker, args, attribute, newTreeNode.Nodes);
            }
        }
        foreach (XmlNode childNode in xmlNode.ChildNodes)
        {
            ConvertXmlNodeToTreeNode(worker, args, childNode, newTreeNode.Nodes);
        }
    }