I am new to this project. It is on visual studio 2010 in C#. I am trying to show this XML schema i开发者_开发问答n a textbox with a treeview. I have problem making it into a treeview.
DataSet dsM= new DataSet("M"); string filePath = @"C:\Documents and Settings\Administrator\Desktop\M.xsd"; dsM.ReadXml(filePath); System.IO.StringWriter swXML = new System.IO.StringWriter(); dsM.WriteXmlSchema(swXML); textBox.Text = swXML.ToString();
using System;
using System.Windows.Forms;
using System.Xml;
public class XmlTreeDisplay : System.Windows.Forms.Form{
private System.Windows.Forms.TreeView treeXml = new TreeView();
public XmlTreeDisplay() {
treeXml.Nodes.Clear();
this.Controls.Add(treeXml);
// Load the XML Document
XmlDocument doc = new XmlDocument();
string filePath = @"C:\Documents and Settings\Administrator\Desktop\M.xsd";
try {
doc.Load(filePath);
}catch (Exception err) {
MessageBox.Show(err.Message);
return;
}
ConvertXmlNodeToTreeNode(doc, treeXml.Nodes);
treeXml.Nodes[0].ExpandAll();
}
private void ConvertXmlNodeToTreeNode(XmlNode xmlNode,
TreeNodeCollection treeNodes) {
TreeNode newTreeNode = treeNodes.Add(xmlNode.Name);
switch (xmlNode.NodeType) {
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.XmlDeclaration:
newTreeNode.Text = "<?" + xmlNode.Name + " " +
xmlNode.Value + "?>";
break;
case XmlNodeType.Element:
newTreeNode.Text = "<" + xmlNode.Name + ">";
break;
case XmlNodeType.Attribute:
newTreeNode.Text = "ATTRIBUTE: " + xmlNode.Name;
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
newTreeNode.Text = xmlNode.Value;
break;
case XmlNodeType.Comment:
newTreeNode.Text = "<!--" + xmlNode.Value + "-->";
break;
}
if (xmlNode.Attributes != null) {
foreach (XmlAttribute attribute in xmlNode.Attributes) {
ConvertXmlNodeToTreeNode(attribute, newTreeNode.Nodes);
}
}
foreach (XmlNode childNode in xmlNode.ChildNodes) {
ConvertXmlNodeToTreeNode(childNode, newTreeNode.Nodes);
}
}
public static void Main(){
Application.Run(new XmlTreeDisplay());
}
}
modify this to suit your needs...
(to do the reverse, convert a TreeView to XML, here is a nice demonstration not necessary repeating here)
精彩评论