XML Extensible Markup Language

Source: Internet
Author: User

-----------------20150123 XML Extensible Markup Language--------------------------------------
XML Extensible Markup Language
XML: Storing data
Attention:
The XML is strictly case-sensitive.
XML tags also appear in pairs.
XML documents have only one root node.
Node
Elements

<?xml version= "1.0" encoding= "Utf-8"?>
<Books>
<Book>
<Name> Gold **</name>
<Price>10</Price>
<Des> good-looking, not explained. </Des>
</Book>
</Books>

To create an XML document from code
1. Referencing a namespace using System.Xml
2. Creating an XML Document Object
XmlDocument doc=new XmlDocument ();
3. Create the first line description information and add it to the doc document
XmlDeclaration Dec=doc. Createxmldeclaration ("1.0", "Utf-8", null);
Doc. AppendChild (DEC);
4. Create the root node
XmlElement Books=doc. CreateElement ("Books");
To add a root node to a document
Doc. Appendchile (books);
5. Create a child node for the root node books
XmlElement Book1=doc. CreateElement ("book");
Adding Book1 to the root node
Books. AppendChild (BOOK1);

6. Add child nodes to Book1
XmlElement Name1=doc. CreateElement ("Name");
Name1. innertext= "Gold * *";
Book1. AppendChild (NAME1);

XmlElement Price1=doc. CreateElement ("Price");
Price1. innertext= "10";
Book1. AppendChild (Price1);

XmlElement Des1=doc. CreateElement ("Des");
Des1. innertext= "Good looking";
Book1. AppendChild (DES1);


Doc. Save ("books.xml");
Console.WriteLine ("Saved successfully");
Console.readkey ();
------------------------------------------------------------------
<?xml version= "1.0" encoding= "Utf-8"?>
<Order>
<CustomeName> Liu Yang </CustomeName>
<CustomerNumber> Liu Yang </CustomerNumber>
<Items>
<orderitem name= "Code table" count= "2"/>
<orderitem name= "Raincoat" count= "Max"/>
<orderitem name= "Glove" count= "1"/>
</Items>
</Order>

To create an XML document with attributes
XmlDocument doc=new XmlDocument ()
XmlDeclaration Dec=doc. Createxmldeclaration ("1.0", "Utf-8", null);
Doc. AppendChild (DEC);

XmlElement Order=doc. CreateElement ("Order");
Doc. AppendChild (order);

XmlElement Customername=doc. CreateElement ("CustomerName");
customername.innertext= "Geng Shiwei";
Order. AppendChild (CustomerName);

XmlElement Customernumber=doc. CreateElement ("CustomerNumber");
customernumber.innertext= "100001";
Order. AppendChild (CustomerNumber);

XmlElement Items=doc. CreateElement ("Items");
Order. AppendChild (items);

XmlElement Orderitem1=doc. CreateElement ("OrderItem");
Adding attributes to a node
Orderitem1.setattribute ("Name", "Inflatable Doll");
Orderitem1.setattribute ("Count", "10");
Items. AppendChild (ORDERITEM1);

XmlElement Orderitem2=doc. CreateElement ("OrderItem");
Adding attributes to a node
Orderitem2.setattribute ("Name", "Inflatable Doll");
Orderitem2.setattribute ("Count", "10");
Items. AppendChild (ORDERITEM2);

XmlElement Orderitem3=doc. CreateElement ("OrderItem");
Adding attributes to a node
Orderitem3.setattribute ("Name", "Inflatable Doll");
Orderitem3.setattribute ("Count", "10");
Items. AppendChild (ORDERITEM3);

Doc. Save ("Order.xml");


-----------------------------Append XML Document------------------------------------------------------
Append XML document
XmlDocument doc=new XmlDocument ();
XmlElement Books;
if (file.exists ("books.xml")) {
If the file exists with the left XML
Doc. Load ("books.xml");
Gets the root node of the file
Books=doc. DocumentElement;
}else{
If the file does not exist
Create first row
XmlDeclaration Dec=doc. Reatexmldeclaration ("1.0", "Utf-8", null);
Doc. AppendChild (DEC);
Creating the root node
Books=doc. CreateElement ("Books");
Doc. AppendChild (books);
}
XmlElement Book1=doc. CreateElement ("book");
Adding Book1 to the root node
Books. AppendChild (BOOK1);

XmlElement Name1=doc. CreateElement ("Name");
Name1. Innertext= "C # development Daquan";
Book1. AppendChild (NAME1);

XmlElement Price1=doc. CreateElement ("Price");
Price1. innertext= "101";
Book1. AppendChild (Price1);

XmlElement Des1=doc. CreateElement ("Des");
Des1. Innertext= "Can not understand";
Book1. AppendChild (DES1);

Doc. Save ("books.xml");


------------------------4. Read the XML document--------------------------------------
XmlDocument doc=new XmlDocument ();
Load the XML to be read
Doc. Load ("books.xml");
Get root node
XmlElement Books=doc. DocumentElement;
Gets the child node that returns the collection of nodes
XmlNodeList Xnl=books. ChildNodes;
foreach (XmlNode item in XNL)
{
Console.WriteLine (item. InnerText);
}
Console.readkey ();


----------------------5, read the XML document with attributes-----------------------------------
XmlDocument Doc =new XmlDocument ();
Doc. Load ("Order.xml");
XmlNodeList Xnl=doc. SelectNodes ("/order/items/orderitem");
foreach (XmlNode node in xnl)
{
Console.WriteLine (node. attributes["Name"]. Value);
Console.WriteLine (node. attributes["Count"]. Value);
}


---------------------6, delete the node---------------------------------------------

XmlDocument Doc =new XmlDocument ();
Doc. Load ("Order.xml");
XmlNode Xn=doc. selectSingleNode ("/order/items");
Xn. RemoveAll ();
Doc. Save ("Order.xml");

Console.WriteLine ("Delete succeeded");

XML Extensible Markup Language

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.