I have two XML
files which I show then in the treeview
format in Visual studio C#. In both treeviews
the root and first child is the same. Let display it as:
root
child1
children1
children1
children1
children1
children1
and for the second treeview I have:
root
child1
children2
children2
children2
children2
children2
Now I compare both treeview in the level children
. If they are equal in the name then I leav them otherwise I want to create an virtual node/children but do not know how. I wrote this code but it adds to the root
node instead of adding to the child
node
XmlDocument docXml1 = new XmlDocument();
docXml1.Load(xmlfile1);
XmlDocument docXml2 = new XmlDocument();
docXml2.Load(xmlfile2);
XmlNodeList actions1 = root1.SelectNodes("/root/child1/children1");
XmlNodeList actions2 = root2.SelectNodes("/root/child1/children2");
if (Name_of_children1 != Name_of_children2)
{
var VirtualNode = "";
treeView1.Nodes.Add(VirtualNode.Trim());
}
of course lots of code lines are deleted to make it short.
This is a complete example on how you could do it:
TestForm.cs:
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
this.Load += Test_Load;
}
private void Test_Load(object sender, EventArgs e)
{
try
{
XmlDocument doc1 = new XmlDocument();
doc1.Load("file1.xml");
XmlDocument doc2 = new XmlDocument();
doc2.Load("file2.xml");
trvLeft.Nodes.Clear();
trvRight.Nodes.Clear();
trvLeft.Nodes.Add(new TreeNode("File 1"));
trvRight.Nodes.Add(new TreeNode("File 2"));
TreeNode tlNode = new TreeNode();
TreeNode trNode = new TreeNode();
tlNode = trvLeft.Nodes[0];
trNode = trvRight.Nodes[0];
AddNode(doc1.DocumentElement, doc2.DocumentElement, tlNode, trNode);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
var childrensLeft = trvLeft.Nodes[0].Nodes[0].Nodes;
var childrensRight = trvRight.Nodes[0].Nodes[0].Nodes;
for (int i = 0; i < Math.Min(childrensLeft.Count, childrensRight.Count); i++)
{
if (childrensRight[i].Text != childrensLeft[i].Text)
{
//childrensLeft[i].ForeColor = Color.Red;
//childrensRight[i].ForeColor = Color.Red;
childrensLeft[i].Nodes.Add("Something to left");
childrensRight[i].Nodes.Add("Something to right");
}
}
trvLeft.ExpandAll();
trvRight.ExpandAll();
}
private void AddNode(XmlNode leftXmlNode, XmlNode rightXmlNode, TreeNode leftNode, TreeNode rightNode)
{
//XmlNode xNode;
TreeNode tlNode;
TreeNode trNode;
XmlNodeList lnodeList;
XmlNodeList rnodeList;
int i;
if (leftXmlNode.HasChildNodes && rightXmlNode.HasChildNodes)
{
lnodeList = leftXmlNode.ChildNodes;
rnodeList = rightXmlNode.ChildNodes;
for (i = 0; i <= Math.Min(lnodeList.Count, rnodeList.Count) - 1; i++)
{
var lNode = leftXmlNode.ChildNodes[i];
var rNode = rightXmlNode.ChildNodes[i];
leftNode.Nodes.Add(new TreeNode(lNode.Name));
tlNode = leftNode.Nodes[i];
rightNode.Nodes.Add(new TreeNode(rNode.Name));
trNode = rightNode.Nodes[i];
AddNode(lNode, rNode, tlNode, trNode);
}
}
else
{
leftNode.Text = (leftXmlNode.OuterXml).Trim();
rightNode.Text = (rightXmlNode.OuterXml).Trim();
}
}
}
file1.xml
<root>
<child1>
<children1/>
<children1/>
<children1/>
<children1/>
<children1/>
<children1/>
</child1>
</root>
file2.xml
<root>
<child1>
<children1/>
<children2/>
<children1/>
<children1/>
<children1/>
<children1/>
</child1>
</root>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.