Individual code library external XML operation Drill

Source: Internet
Author: User
 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;


}
*/


}
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.