Nbl Bz

Nbl Bz

  • NA
  • 56
  • 12.8k

select node in treeView and save in XML file

Oct 24 2014 2:50 AM
Hello ,
Am still working on my first C# winforms application .
I populate a TREEVIEW with data from a XML file .
My xml file is like this

<?xml version="1.0" encoding="UTF-8"?>
<source rt="2012"  ez= "123">
<start  id="001" /> 
<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>
</source>


I CAN POPULATE DATA IN MY TREEVIEW

My next step is te choose some node ( with attributes ) and  write them in a new XML file ( or an existing one ) similar to the XML source file
this line dont change in the new file
<?xml version="1.0" encoding="UTF-8"?>
< source rt="2012"  ez= "123">
< start  id="001" />

A wanna just choose object with all childs , or only child and put them in the xml file


they are some exp in the net , i used them but it's not ok , some errors which i don't understand :(

Am working with this code :



public void SerializeTreeView(TreeView treeView, string fileName)


        {


           


XmlWriterSettings settings = new XmlWriterSettings();


            settings.Indent =


true;



           


XmlWriter textWriter = XmlWriter.Create(@"C:\Users\buenb\Documents\projet\EEE.XML", settings);



           


// Writing the xml declaration tag


            textWriter.WriteStartDocument();



           


// Save the nodes, recursive method


            SaveNodes(treeView.Nodes, textWriter);



// End the xml document

            textWriter.WriteEndDocument();

            textWriter.Close();


        }


private void SaveNodes(TreeNodeCollection nodesCollection, XmlWriter textWriter)


        {


for (int i = 0; i < nodesCollection.Count; i++)


            {


               


TreeNode node = nodesCollection[i];


if (node.Nodes.Count > 0)

                {

                    textWriter.WriteStartElement(node.Text);

               }

else


                {


                    textWriter.WriteAttributeString(node.Text,


"Attribute value");


                }



               


if (node.Nodes.Count > 0)


                    SaveNodes(node.Nodes, textWriter);



               


if (node.Nodes.Count > 0)


                    textWriter.WriteEndElement();


            }


        }




Some one can help me to start plz !!
plz !!


thank u all