Before we discuss the usage of XML, let's take a look at the concept of XML.
XML eXtensible Markup Language (Extensible Markup Language) is a W3C recommendation standard used to transmit and store data. XML is widely used and widely used.ProgramIs the most commonly used tool for data transmission .. The core of net is XML, which is an important part of Microsoft's. NET strategy. the. NET Framework (ASP. NET, Web Service, and so on) uses XML as the data representation.
Next we will make a list of common XML knowledge points.
1. Why Learning XML
XML is easy to read, easy to use, and structured, so we often transmit data through XML.
2. XML syntax Specification
Tag ):
001. The name must start with a letter, "_", or ":", but the colon is not recommended.
002. The name cannot contain spaces.
003. The name can also contain English letters, numbers, "_", "-", and ".".
004. The root tag must be unique.
005. Tag pairing
006. tags are case sensitive.
Nest: tags must be reasonably nested and cannot be staggered.
Attribute)
Property Syntax: property name = "property value"
3. XML structure (XML tree)
Similar to the tree structure: XML can have parent nodes, child nodes, and sibling nodes.
4. How do I hand over an XML file?
First, declare the XML: <? XML version = "1.0"
Encoding = "gb2312"?>
Comment in XML: <! --
-->
Common XML writing methods:
- Using dom (Document Object Model)
- LINQ to XML
In fact, the two methods are similar. Here we will discuss how to write XML using the LINQ to XML method.
Common Types
Xelement, xdocument, xattribute, and xnode.
UseCodeWrite XML files
001. Create an XML document
Xdocument
Doc = new xdocument ();
002. Create a root node xelement and define the node name in the constructor
Xelement xroot = new xelement ("root ");
003. Added to XML document
Doc. Add (xroot );
004. Add attributes
// Add class attributes
Xelement
Xclass = new xelement ("class ");
Xattribute
Xdisplay = new xattribute ("display", ". Net ");
Xclass. Add (xdisplay );
// Add student attributes
Xelement
Xperson = new xelement ("person ");
Xattribute
XP = new xattribute ("ID", "0001 ");
Xperson. Add (XP );
005. Add a node
// Student data
Xelement
Xname = new xelement ("name ");
Xname. value = "Li Xiaoling ";
Xelement
Xage = new xelement ("Age ");
Xage. value = "18 ";
Xelement
Xgender = new xelement ("gender ");
Xgender. value = "male ";
// Add it to the corresponding attribute
Xperson. Add (xname );
Xperson. Add (xage );
Xperson. Add (xgender );
Xclass. Add (xperson );
Xroot. Add (xclass );
006. Save the data
Doc. Save ("myfirst. xml ");
The above describes how to hand-write an XML file. Next, let's take a look at how to read an XML file.
5. Read XML files
01. Load the document
Use the xdocument. Load () method
Xdocument Doc = xdocument. Load ("myfirst. xml ");
02. Get the root node
Root attribute
Xelement
Root = Doc. root;
03. Get the element Tag Name and Value
Foreach
(Xelement ele in
Root. Elements ())
{
String
Xclass = ELE. Name. tostring ();
String
SID = ELE. Attribute ("display"). value;
Console. writeline ("{0} {1}", xclass, Sid );
Foreach
(Xelement item in
Ele. Elements ())
{
String
Xname = item. Name. tostring ();
String
Id = item. Attribute ("ID"). value;
String
Name = item. element ("name"). value;
String
Age = item. element ("Age"). value;
String
Gender = item. element ("gender"). value;
Console. writeline ("{0} {1} {2} {3} {4} \ t", xname, ID, name, age, gender );
}
}
6. XML serialization
So how can we serialize objects in the memory into XML files?
We can use the XML serializer. First, we need to introduce the namespace,
Using system. xml. serialization;
The following code is used to generate a list <person> serialized into an XML file.
Private void button#click (Object
Sender, eventargs E)
{
List <person> List = new
List <person> ();
List. Add (New
Person ("Jackie Chan", 18, 'male '));
List. Add (New
Person ("bruce lee", 25, 'male '));
List. Add (New
Person ("Gong Li", 30, 'female '));
// Start serialization below
Xmlserializer
Xml = new xmlserializer (typeof (list <person> ));
Filestream
FS = new filestream ("weilengdeyu. xml", filemode. openorcreate );
XML. serialize (FS, list );
}
Private
Void form1_load (Object
Sender, eventargs E)
{
// MessageBox. Show (typeof (list <person>). tostring ());
}
}
[Serializable]
Public class person
{
Public
Person (string name, int
Age, char gender)
{
This. Name
= Name;
This. Age
= Age;
This. Gender
= Gender;
}
Public
Person ()
{
}
Private
Char gender;
Public char gender
{
Get
{Return gender ;}
Set
{Gender = value ;}
}
Private
Int age;
Public int age
{
Get
{Return age ;}
Set
{Age = value ;}
}
Private
String name;
Public string name
{
Get
{Return name ;}
Set
{Name = value ;}
}
7. Finally contribute to a X-DOM Core Map