469,621 Members | 1,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

updating xcml config file with treeView nodes

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

Aug 28 '06 #1
1 2372
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[i];
}
else
{
node = dom.CreateElement(tempXpath[i]);
// if node does not exist, append node
parent.AppendChild(node);
}

// perform step
i++;
}

Aug 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by serge calderara | last post: by
4 posts views Thread by alanrn | last post: by
5 posts views Thread by Mark Goldin | last post: by
4 posts views Thread by Ian Powell | last post: by
3 posts views Thread by Jan Wrage | last post: by
1 post views Thread by Christian Rühl | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.