472,805 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Update XML 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 3174
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
3
by: Hrvoje Voda | last post by:
I made my own user treeview control. Now, I can't access the method treeview.Nodes ! Why? Hrcko
16
by: Timm | last post by:
I'm trying to use ASP objects (basically formed and populated based on Web.Config settings) and I want to use them in a different non-asp program with minimal reprogramming. So, my question is how...
4
by: J.B. | last post by:
Hi Can anybody show me how to loop all treeview nodes ini vb.net. I've been working on this for days but still no result. I got a sample code in vb 6.0, this one works. Private Sub SaveNested(...
3
by: juvi | last post by:
Hi, I have got a problem with Treeview.Nodes.Clear() under VB2005. When I have some nodes in my treeview and a force to clear() all nodes then it seems to work, because the nodes are not visible....
0
by: hazz | last post by:
I want to load Service.exe.config into a DataGrid, change the timer interval value from 10000 to 555555, then update the original exe.config. The code below shows the code at step 1 and 2 before...
1
by: Christian Rühl | last post by:
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...
4
by: sajidali | last post by:
Hi to all of you? the problem is that i am working on a project which will generate the treeview nodes dynamically. i mean number of treeview nodes will be equal to the number of lines in a text...
0
by: Selva Kumari Padmanabhan | last post by:
hi, i am a new to c#, i hav 4months experience in C#, I am populating a treeview in c# from XmlFile, and i have implemented a multiselection in that treeview by overriding treeview select...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.