0

我需要两个从两个 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>
4

1 回答 1

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);
        }
    }
于 2014-04-25T14:00:18.613 回答