updating xcml config file with treeView nodes

  • Thread starter =?iso-8859-1?q?Christian_R=FChl?=
  • Start date
?

=?iso-8859-1?q?Christian_R=FChl?=

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;
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;
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
 
?

=?iso-8859-1?q?Christian_R=FChl?=

okay, i advanced a little bit solving this problem.

i split up the treenodes xpath and put its fragments into an array.
now i walk through this array and check for each xpath fragment if a
correspondent node in my config.xml file exists, adding fragment to
fragment to a part-path so to speak.

works all well until i come to a point where i have to append a new
child to my parent node.
i don't get an error, but it still does not work. i really need help
with this now.

while(i<=tempXpath.Length)
{
node = dom.SelectSingleNode(temp);
parent = dom.SelectSingleNode(oldTemp);

if(XmlNodeType.IsInstanceOfType(node))
{
// if node exists, check for child node
oldTemp = temp;
temp = temp + "/" + tempXpath;
}
else
{
node = dom.CreateElement(tempXpath);
// if node does not exist, append node
parent.AppendChild(node);
}

// perform step
i++;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top