473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3238
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.