读取两个xmls并在Windows应用程序中创建treeview控件

时间:2013-05-03 06:16:07

标签: c# xml xml-parsing xml-deserialization

我需要两个动态创建两个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>

1 个答案:

答案 0 :(得分:0)

以下是C#实现。 由于它在BackgroundWorker线程中工作,如果您不使用,请将其删除。

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