PHP expert |
|
Ik snap de theorie wel, maar ik heb het niet voor elkaar gekregen, heb je misschien een heel simpel voorbeedje? Ik heb het nu zo opgelost. is dit een goede oplossing of is er een betere?
private void Form1_Load(object sender, EventArgs e)
{
try
{
System.Xml.XmlDocument document = new XmlDataDocument();
document.Load("blueprints.xml");
populateTreeControl(document.DocumentElement, treeView1.Nodes);
treeView1.ExpandAll();
}
catch (XmlException x)
{
MessageBox.Show(x.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void populateTreeControl(XmlNode document, TreeNodeCollection nodes)
{
foreach (XmlNode node in document.ChildNodes)
{
TreeNode type = null;
TreeNode race = null;
if (nodes.ContainsKey(node.Attributes["type"].Value) == false)
{
type = nodes.Add(node.Attributes["type"].Value, node.Attributes["type"].Value);
}
else
{
type = nodes[node.Attributes["type"].Value];
}
if (type.Nodes.ContainsKey(node.Attributes["race"].Value) == false)
{
race = type.Nodes.Add(node.Attributes["race"].Value, node.Attributes["race"].Value);
}
else
{
race = nodes[node.Attributes["race"].Value];
}
race.Nodes.Add(node.Attributes["name"].Value, node.Attributes["name"].Value);
}
}
private void Form1_Load(object sender, EventArgs e) { try { System.Xml. XmlDocument document = new XmlDataDocument (); document.Load("blueprints.xml"); populateTreeControl(document.DocumentElement, treeView1.Nodes); treeView1.ExpandAll(); } catch (XmlException x) { MessageBox.Show(x.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void populateTreeControl(XmlNode document, TreeNodeCollection nodes) { foreach (XmlNode node in document.ChildNodes) { TreeNode type = null; TreeNode race = null; if (nodes.ContainsKey(node.Attributes["type"].Value) == false) { type = nodes.Add(node.Attributes["type"].Value, node.Attributes["type"].Value); } else { type = nodes[node.Attributes["type"].Value]; } if (type.Nodes.ContainsKey(node.Attributes["race"].Value) == false) { race = type.Nodes.Add(node.Attributes["race"].Value, node.Attributes["race"].Value); } else { race = nodes[node.Attributes["race"].Value]; } race.Nodes.Add(node.Attributes["name"].Value, node.Attributes["name"].Value); } }
|