OpenFileDialog odlg = new OpenFileDialog();
odlg.Title = "Select a XML file";
odlg.Filter = "XML File|*.xml;*.bmp;*.gif|All File|*.*";
if (odlg.ShowDialog() != DialogResult.Cancel)
{
Application.DoEvents();
txt_XMLFileName.Text = odlg.FileName;
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(odlg.FileName);
tvw_XML.Nodes.Clear();
foreach (System.Xml.XmlNode xn in xDoc.ChildNodes)
{
System.Xml.XmlNode xnx = xn;
TreeNode ttn = tvw_XML.Nodes.Add(xn.Name);
LoadNode(ref ttn,ref xnx);
}
tvw_XML.ExpandAll();
xDoc.Save(odlg.FileName);
}
private void LoadNode(ref TreeNode tn,ref System.Xml.XmlNode xn1)
{
foreach (System.Xml.XmlNode xn in xn1.ChildNodes )
{
System.Xml.XmlNode xnx = xn;
if (xn.Name == "#text")
{
TreeNode ttt = tn.Nodes.Add(xn.Value);
ttt.ForeColor = Color.Blue;
}
else
{
TreeNode ttn = tn.Nodes.Add("<" + xn.Name + ">");
ttn.NodeFont = new Font("Tahoma",8F, FontStyle.Bold);
LoadNode(ref ttn, ref xnx);
}
}
}
}
No comments:
Post a Comment