Xml learning Summary-C # operating Xml and Javascript operating xml

Source: Internet
Author: User

// <A name = "文3 3 3" value = "; 3">
// <B name = "corporate Photo 3" value = "Chinese; 3; corporate Photo 3"/>
// </A>
String directoryName = "test ";
Int id = 0;
String treexml = Server. MapPath ("TreeXml. xml ");
// 1. Define an XML Object
XmlElement tA = null, tB = null, root = null;
XmlDocument xmldoc = new XmlDocument ();
Try
{
// 2. Load the configuration file to the XmlDocument object through the path
Xmldoc. Load (treexml );
// 3. Get the document root
Root = xmldoc. DocumentElement;
XmlNodeList nodelist = root. SelectNodes ("/root/A [name = 'photo 3']");
Id = nodelist. Count;

// 4. Create an object and add attributes
TA = xmldoc. CreateElement ("");
TA. SetAttribute ("name", directoryName );
TA. SetAttribute ("value", directoryName + ";" + id );
TB = xmldoc. CreateElement ("B ");
TB. SetAttribute ("name", directoryName );
TB. SetAttribute ("value", directoryName + ";" + id + ";" + directoryName );
TA. AppendChild (tB );
Root. AppendChild (tA );

Xmldoc. Save (treexml );
}
Catch (Exception)
{
}

SelectNodes and XPath

XPath is the content of XML. Here SelectNodes is a method of XmlDocument or XmlNode in C. SelectNodes uses XPath to select nodes.

Important syntax

SelectNodes ("item ")

Select the node named item from the current node's son node (only son, not including Sun Tzu and Sun Tzu.

SelectNodes ("/item ")

Select the node named item from the child node of the root node.

SelectNodes ("// item ")

Select a node named item from any node. To highlight this arbitrary position, it is not affected by the current node. That is to say, if the current node is at Layer 100th (exaggerated), you can also select the node named item at the first layer.

SelectNodes (".")

Select the current node.

SelectNodes ("..")

Select the parent node of the current node.

SelectNodes ("// item [@ name]")

Based on SelectNodes ("// item"), a restriction is added, that is, the property of name is required.

SelectNodes ("// item [@ name = '000000']")

Based on SelectNodes ("// item [@ name]"), a restriction is added, that is, the attribute value of name must be 111. Note that there are quotation marks in the syntax. If there is no quotation marks, it indicates the number type. You can use numbers larger than or less than numbers, for example: selectNodes ("// item [@ v> 333]").

SelectNodes ("// item [1]")

Select the first item. Note that it is the first item, not the second item.

In addition, this refers to the first item between siblings. That is to say, if the parent level has three items, select the first item; if two sons with the same name as item exist in the second item of the parent level, the first son will be selected; if the third item of the parent class has two sons with the same name as the item, the first son will also be selected ......

SelectNodes ("// item [last ()-1]")

The second-to-last Node also refers to the second-to-last node between siblings.

SelectNodes ("// item [position () <= 2]")

The first and second nodes (the position () of the first node is 1.

SelectNodes ("// @ name ")

SelectNodes ("/root/item/@ name") obtains the name attribute of an item.

Select the name attribute. Note that the attribute is selected instead of the node. Use the Value property to obtain the property Value of the property set.

SelectNodes ("/root/item ")

The item son node under the root node.

SelectNodes ("/root // item ")

Under the root node, whether it is a son, grandson, or grandson ......, All items named item are selected.

Wildcard

You can use * to indicate any node name, such as SelectNodes ("/channel/*/item ");
Use @ * to indicate any attribute;
Use node () to represent any type of node;
Text () indicates a node of the text type, which is actually a node of the text. (The element node is a text node, and the text node is a text node ).
Operator

| Represents or. SelectNodes ("// item | // channel"), and select the item or channel node.

 

 

--------------

/****** 1, Content-Type
In many cases, the Content-Type issue cannot be resolved.
Skip this step if it is an xml file.
The dynamically generated XML must be set to text/xml. Otherwise, text/html is the common text by default.
Set Content-Type in common languages *********/

Header ("Content-Type: text/xml"); // php
Response. ContentType = "text/xml" // asp
Response. setHeader ("ContentType", "text/xml"); // jsp
/******
2. xml structure.
XML must be closed. It is very important!
Example:
Incorrect XML *********/

<? Xml version = "1.0" encoding = "UTF-8"?>
<Name> zhangsan </name>
<Id> 1 </id>
<Name> lisi </name>
<Id> 2 </id>

// Correct

<? Xml version = "1.0" encoding = "UTF-8"?>
<Stulist>
<Student email = "1@1.com">
<Name> zhangsan </name>
<Id> 1 </id>
</Student>
<Student email = "2@2.com">
<Name> lisi </name>
<Id> 2 </id>
</Student>
</Stulist>

/******
3. Parsing
Here, macnie's
Traverse student (here we still use the above XML and the sub-node is student )*********/

$. Ajax ({
Http://www.cnblogs.com/xiaopin/admin/'ajax.asp ',
Type: 'get ',
DataType: 'xml', // do not write text or html here !!!
Timeout: 1000,
Error: function (xml ){
Alert ('error loading XML document '+ xml );
},
Success: function (xml ){
$ (Xml). find ("student"). each (function (I ){
Var id = $ (this). children ("id"); // retrieves an object
Var idvalue = $ (this). children ("id"). text (); // get text
Alert (id_value); // here is the ID value.
Alert ($ (this). attr ("email"); // The email attributes under student are displayed here.
// The final output. This is the CSS rain method, which seems to be a little more JQ than macnie.
$ ('<Li> </li> ')
. Html (id_value)
. AppendTo ('ol ');
});
}
});

// Add one last line: ensure that the server side is UTF-8 encoded; otherwise, garbled characters will occur! Make sure that your xml file is in UTF-8 format.

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.