We usually see XML files like this.
<? XML version = "1.0"?>
{
Return false
} "Href =" http://writeblog.csdn.net/# "onfocus =" function onfocus ()
{
H ()
} ">
-<Sqlconfig>
The SQL configuration information is saved here!
<Ip>
192.168.0.250</IP>
<Userid>
SA</Userid>
<Password>
SA</Password>
<Dbname>
Hhfrm</Dbname>
<Bak_path>
E:/backup</Bak_path>
</Sqlconfig>
When using XML, there are several basic requirements for literacy:
Xmldocument class: XML document, as its name implies. One of its objects represents an XML document. The above document is an xmldocument object.
Xmlelement class: an element in an XML document. For example, the IP address and userid are all elements.
Xmltext: XML text. For example, "the SQL configuration information is saved here" is a text.
Xmlnode class: indicates a node, which can be an element or a document.
How to create an XML document in the example:
// Initialize the XML document
Xmldocument Doc = new xmldocument ();
// Initialize the Element Node
Xmlelement xmlele_sqlconfig, xmlele_ip, xmlele_userid, xmlele_password, xmlele_dbname, xml_bak_path;
// Initialize the text node
Xmltext;
// Add the first element
Xmlele_sqlconfig = Doc. createelement ("sqlconfig"); // initialize the xmlele_sqlconfig Element
Xmltext = Doc. createtextnode ("the SQL configuration information is saved here! "); // Initialize a text node
Xmlele_sqlconfig.appendchild (xmltext); // Add the text node to the xmlele_sqlconfig element.
Doc. appendchild (xmlele_sqlconfig); // Add the xmlele_sqlconfig element to the XML text.
// Add a child element to the first element
Xmlele_userid = Doc. createelement ("userid"); // initialize the xmlele_userid element.
Xmltext = Doc. createtextnode (userid); // initialize the text node
Xmlele_userid.appendchild (xmltext); // Add the text node to the xmlele_userid element.
Xmlele_sqlconfig.appendchild (xmlele_userid); // Add the xmlele_userid element to the xmlele_sqlconfig element.
The other nodes will not be repeated .. There is also a way to add elements.
// Method 2 for adding a child element to the first element
Xml_bak_path = Doc. createelement ("bak_path"); // initialize the bak_path element.
Xml_bak_path.innertext = bak_path; // set text for the bak_path element. Instead, initialize the text first, and then add the text to the element.
Xmlele_sqlconfig.appendchild (xml_bak_path); // Add the xmlele_userid element to the xmlele_sqlconfig element.
Read:
// In the element sqlconfig, find the element named IP. And extract the value of its first child node (it may be the element or text, so it still needs innertext ).
String IP = sqlconfig. getelementsbytagname ("ip"). Item (0). innertext;