Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. xml;
Using system. IO;
Namespace windowsformsapplication1
{
Public partial class frm_mainform: Form
{
// String filename = NULL;
Public frm_mainform ()
{
Initializecomponent ();
}
Private void btnnew_click (Object sender, eventargs E)
{
Savefiledialog Sf = new savefiledialog ();
SF. Title = "Creating an XML file ";
SF. Filter = "XML file (*. XML) | *. xml ";
SF. showdialog (this );
Mymethod_xmlwriter (SF. filename );
Rtb_editor.text = file. readalltext (SF. filename );
}
Private void mymethod_xmlwriter (string filename)
{
If (filename! = "")
{
Groupbox1.enabled = true;
// Create an XML Document Object
Xmldocument xmldoc = new xmldocument ();
// Add the XML declaration.
Xmldoc. appendchild (xmldoc. createxmldeclaration ("1.0", "UTF-8", "yes "));
// Add XML annotations.
Xmldoc. appendchild (xmldoc. createcomment ("comment "));
// Add empty rows.
Xmldoc. appendchild (xmldoc. createwhitespace ("\ n "));
// Create a root node
// Xmlelement xmlroot = xmldoc. createelement ("root ");
// Create a root node (with namespace)
Xmlelement xmlroot = xmldoc. createelement ("ye", "root", "http://www.smartsoso.com ");
// Add the root node
Xmldoc. appendchild (xmlroot );
// Add [CDATA data ].
// Xmlroot. appendchild (xmldoc. createcdatasection ("CDATA data "));
// Create a subnode
Xmlelement bgcolor = xmldoc. createelement ("bgcolor ");
Bgcolor. setattribute ("ID", "yeye ");
Bgcolor. innertext = "red"; // The value of the subnode.
Xmlroot. appendchild (bgcolor); // Add it to the root node
// Save it to a file.
Xmldoc. Save (filename );
/*
// Read
// Create an XML Object
Xmldocument myxml = new xmldocument ();
// Read the existing XML
Myxml. Load (filename );
// Declare a node to store the root node
Xmlnode movie = myxml. documentelement;
// Traverse the child nodes under the root node
Foreach (xmlnode VaR in movie. childnodes)
{
// Console. writeline (var. Name); // obtain the name of the root node.
// Console. writeline (var. innertext); // obtain the value of the root node!
Rtb_editor.text + = var. Name;
Rtb_editor.text + = var. innertext;
}
*/
}
}
}
}