c# - To read two xmls and create treeview control in windows application -
i need 2 create dynamically tree view 2 xmls:
from first xml parent node project a(that text of node ) , child nodes folders till emails second xml , path of second xml in first xml.
please stuck?
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 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 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 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 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 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 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>
following c# implementation. since it's working in backgroundworker thread, please remove it, if not use.
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); } }
Comments
Post a Comment