Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

Am working on my first Winforms application , then I need to select or check some nodes in treeview and save them in a new xml file ( or existing one )

My XML file source is like this one :

XML
<?xml version="1.0" encoding="UTF-8"?>
<object_pool rt="2012"   ez= "123">
<category active="0" attribut1="vrai" titre="aaaaaa">
    <category_value titre="acac">
      <object  background_colour="230" id="0" name="zzzzzz"  type="0">
        <child id="121" x="2" y="0" />
        <child id="131" x="3" y="0" />
      </object>
</category_value>
</<category>
</object_pool>


then for exp I want to choose only object or only child or some objects if i have a lot ...

am using this function but it refer to me all nodes :
C#
private StreamWriter sr;

public void exportToXml(TreeView tv, string filename)
{
    sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
    //Write the header
    sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");

    //Write our root node
    sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
    foreach (TreeNode node in tv.Nodes)
    {
    
    
       saveNode(node.Nodes);
    }
    //Close the root node
    sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");

    sr.Close();
}

private void saveNode(TreeNodeCollection tnc)
{
    foreach (TreeNode node in tnc)
    {
        // If we have child nodes, we'll write
        // a parent node, then iterate through
        // the children
        if (node.Nodes.Count > 0)
        {
           sr.WriteLine("<" + node.Text + ">");
           saveNode(node.Nodes);
           //sr.WriteLine("<" + node.Text + ">");
           sr.WriteLine("<" + node.Name + ">");
        }
        else //No child nodes, so we just write the text
        {
           sr.WriteLine(node.Text);
        }
    }
}




I really need to do it , thanks u all for help
Posted
Updated 29-Oct-14 0:01am
v3

Don't use streamwriter to create XML files. Use the .NET XmlWriter class[^].
 
Share this answer
 
Comments
Member 11134330 29-Oct-14 5:50am    
I didn't use it before :( what is the difference between them ?
Richard MacCutchan 29-Oct-14 6:20am    
The XmlWriter class will make sure your XML file is correctly structured. It saves you from having to manage opening and closing of nodes. The .NET classes are there to help you create well-formed applications.
Member 11134330 29-Oct-14 6:21am    
Ok thanks , I will read about how i can use it
If you already use check boxes in your tree view, you can use the property Checked.

Like
C#
foreach (TreeNode node in tnc)
{
    if (node.Nodes.Count > 0)
    {
        if (node.Nodes.Checked)     // Set a breakpoint on this line
            continue;
    
        // The rest of your code
    }
}


As stated in Solution 1, you should use XmlWriter or even XDocument [^](LINQ)

The benefit you get is that you don't have to bother about writing < and > explicitly.
And it will be much easier to create a well formed XML document.

Read the documentation and you will soon see the light.
 
Share this answer
 
v2
Comments
Member 11134330 29-Oct-14 6:23am    
I will read about XmlWriter then modify my code .
Yes i use checkbox but also with
if (node.Checked)
continue;
I have all my node :(
George Jonsson 29-Oct-14 6:30am    
I updated my answer.
I missed that you are looking when step below for some odd reason.
Also set a breakpoint in the code and check the value of node.Nodes.Checked.
Member 11134330 29-Oct-14 6:44am    
Break point : I stop my program in this line ?
with if (node.Nodes.Checked) i have a error : that i have not a definition for checked, then no methode accept as first argument type System.Windows.froms.TreeNodeCollection ...
If you want to save only the current Checked Nodes (wherever they occur), then I suggest you keep track of which ones are Checked/UnChecked by the user at run-time, and then save only those Nodes:
// holds the current Checked Nodes
private List<TreeNode> CheckedNodes = new List<TreeNode>();
    
private void TVChecked_AfterCheck(object sender, TreeViewEventArgs e)
{
    TreeNode checkChangedNode = e.Node;
    
    if (checkChangedNode.Checked)
    {
        if (! CheckedNodes.Contains(checkChangedNode)) CheckedNodes.Add(checkChangedNode);
    }
    else
    {
        if (CheckedNodes.Contains(checkChangedNode)) CheckedNodes.Remove(checkChangedNode);
    }
}

// assume this Button will call the code to save the Checked Nodes
private void btnSaveCheckedNodes_Click(object sender, EventArgs e)
{
    // no Checked Nodes to save ?
    if(CheckedNodes.Count == 0)
    {
        // throw an error ?  return ?
        return;
    }

    // can you save the whole CheckedNodes List in one go ?
    
    // save them one-by-one ?
    //foreach (TreeNode tNode in CheckedNodes)
    //{
        // save each Checked Node
    //}
}
 
Share this answer
 
Comments
Member 11134330 29-Oct-14 8:10am    
Sorry , I am trying to use it ( but am a small strarter so some many question )
I will create event :
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
then put the code , but i have an error ( type definition or name space or end file ).

Then about btnSaveCheckedNode it will be like this :
private void button8_Click_1(object sender, EventArgs e)
{
if(CheckedNodes.Count == 0)
{
// throw an error ? return ?
return;
}
else
exportToXml(treeView1, @"C:\Users\\Documents\projet\EEE.XML");

So I don't need to change anything in my export function ?
BillWoodruff 29-Oct-14 9:29am    
If you wanted to save only the Checked TreeNodes in the 'CheckedNodes list then you would have to write an export function to do just that.

If you want to save the entire TreeView: that's a different question.

If all the TreeNodes are created at design-time, then they will persist in your application, and do not need to be restored.

However, if the user creates new TreeNodes at run-time, or moves the Nodes around with drag-drop, or deletes Nodes, then the current state of the entire TreeView will have to be saved somehow, whether Nodes are Checked or un-Checked.

Those are two very different scenarios.
Member 11134330 29-Oct-14 9:39am    
Now , I can save the entire TreeView , it's not the problem.
Am looking for a solution concerning how to save only CheckedNodes
Basically, I load my data from an XML file into a treeView , then I want to check one or some nodes ( parents or child as XML file exemple ) then save them in a new XML file ( same things like XML source ) . Now I don't need to drop or drag anything .
Member 11192477 30-Oct-14 6:42am    
So , how we can start with this function to export data selected to XML file plz !
someone have an idea ??
exportToXml is not enough to do this ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900