hey! what i wanna do sounds very simple at first, but it turned out to
be a real bone crusher...
i want to check if a treeView node is checked and if a correspondent
node in my xml config file exists just to sort of synchronize them by
changing the xml nodes attribute(s).
somehow i always catch an exception "blabla has an invalid token" but i
cannot find a solution for this. maybe someone of you people can tell
me how to do this correctly.
thanks in advance!
================================================== =============
#region Write config XML and save changes
/// <summary>
/// write config file
/// </summary>
private void WriteConfigXml()
{
XmlDocument configDom = new XmlDocument();
XmlNode configNode;
TreeNode tnode;
// node path in tree
string xpath = null;
try
{
if(File.Exists(m_configFile))
{
m_XmlReader = new XmlParser();
configDom = m_XmlReader.ParseObj(m_configFile);
// set attribute [status="checked"]
XmlAttribute attr = configDom.CreateAttribute("status");
attr.Value="checked";
for(int i=0; i<treeView1.Nodes.Count; i++)
{
// collect xpath of current treeView1 node
tnode = treeView1.Nodes[i];
xpath = tnode.FullPath.ToString();
// check if node is a leaf and if so, go to parent node because a
// leaf in treeView1 does not have any correspondent node in xml
tree
if(tnode.GetNodeCount(true)!=0)
configNode = configDom.SelectSingleNode(xpath);
else
configNode = configDom.SelectSingleNode(xpath).ParentNode;
// Get a XML node type object
Type XmlNodeType = typeof(XmlNode);
// tnode checked and correspondent xml node found?
if(tnode.Checked==true
&& XmlNodeType.IsInstanceOfType(configNode))
{
// append checked status to xml node if not found
if(configNode.Attributes.Count==0)
configNode.Attributes.Append(attr);
}
// tnode not checked and correspondent xml node found?
else if(tnode.Checked==false
&& XmlNodeType.IsInstanceOfType(configNode))
{
// remove checked status from xml node if found
if(configNode.Attributes.Count!=0)
configNode.Attributes.Remove(attr);
}
// tnode checked but no correspondent xml node found?
else if(tnode.Checked==true
&& !XmlNodeType.IsInstanceOfType(configNode))
{
configDom.DocumentElement.AppendChild(configNode);
configNode.Attributes.Append(attr);
}
// tnode not checked and no correspondent xml node found?
else if(tnode.Checked==false
&& !XmlNodeType.IsInstanceOfType(configNode))
{
configDom.DocumentElement.AppendChild(configNode);
}
else
{
MessageBox.Show("Unknown Error while saving changes to config
file! Not saved!");
}
// loop through child nodes
for (int j=0; j<tnode.Nodes.Count; j++)
WriteChildNodes(tnode.Nodes[j], configDom, attr);
}
// save selection changes to config file
configDom.Save(m_configFile);
MessageBox.Show("Änderungen erfolgreich gespeichert!");
}
else
MessageBox.Show("Cofuguration file not found!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("Änderungen konnten nicht gespeichert werden!");
}
}
/// <summary>
/// update treeview and set checkboxes
/// </summary>
private void WriteChildNodes(TreeNode tnode, XmlDocument configDom,
XmlAttribute attr)
{
TreeNode node;
XmlNode configNode;
string xpath = null;
try
{
for(int i=0; i<tnode.Nodes.Count; i++)
{
node = tnode.Nodes[i];
xpath = node.FullPath.ToString();
// check if node is a leaf and if so, go to parent node because a
// leaf in treeView1 does not have any correspondent node in xml
tree
if(tnode.GetNodeCount(true)!=0)
configNode = configDom.SelectSingleNode(xpath);
else
configNode = configDom.SelectSingleNode(xpath).ParentNode;
// Get a XML node type object.
Type XmlNodeType = typeof(XmlNode);
// node checked and correspondent xml node found?
if(node.Checked==true
&& XmlNodeType.IsInstanceOfType(configNode))
{
// append checked status to xml node if not found
if(configNode.Attributes.Count==0)
configNode.Attributes.Append(attr);
}
// node not checked and correspondent xml node found?
else if(node.Checked==false
&& XmlNodeType.IsInstanceOfType(configNode))
{
// remove checked status from xml node if found
if(configNode.Attributes.Count!=0)
configNode.Attributes.Remove(attr);
}
// node checked but no correspondent xml node found?
else if(node.Checked==true
&& !XmlNodeType.IsInstanceOfType(configNode))
{
configDom.DocumentElement.AppendChild(configNode);
configNode.Attributes.Append(attr);
}
// node not checked and no correspondent xml node found?
else if(node.Checked==false
&& !XmlNodeType.IsInstanceOfType(configNode))
{
configDom.DocumentElement.AppendChild(configNode);
}
else
{
MessageBox.Show("Unknown Error while saving changes to config file!
Not saved!");
}
// loop through child nodes
for (int j=0; j<node.Nodes.Count; j++)
WriteChildNodes(node.Nodes[j], configDom, attr);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("Änderungen konnten nicht gespeichert werden!");
}
}
#endregion