Nbl Bz

Nbl Bz

  • NA
  • 56
  • 12.8k

transfer data to xml if node is selected

Oct 27 2014 10:09 AM
hello ,
I m working on an XML file and treeview :
I want to write a new XML file from a data in a tree view , am doing it with this function and it's seems ok :



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 (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);

  }

  }


then fonr the button : 



private void button8_Click_1(object sender, EventArgs e)

  {


exportToXml(treeView1, @"C:\Users\\Documents\projet\EEE.XML");
  

  }



I want to know if it's possible to transfer in this XML file only data in node selected/cheked in my treeView ?
When my node ( or some nodes ) is selected , then in my xml file i find only this one with the same structure .

Am starter, i dont know how to use event , then if i need event to do this or not ...