CYQ. Data. Xml XmlHelper helps you operate Xml/Html more conveniently and quickly.

Source: Internet
Author: User

Preface:

CYQ. Data has added the Xml function since V3.0, starting with the autumn garden, because the autumn garden mainly loads Html operations in Xml mode.

However, for a long time, I have never written any tutorials on the Xml direction. Therefore, this function,It has never been discovered and valued.

The application of Xml in the autumn garden has gone through and processed quite a lot of special environments and special characters and special processing, and is already quite stable.

Therefore, the document is officially written for it. The following is an introduction and example.

 

 

I. Description of XmlHelper class

With this class, you can easily operate Xml/Html.

 

Ii. XmlHelper instructions:

 

1: instantiation:

XmlHelper doc = new XmlHelper (...);

Method prototype:

Public XmlHelper () // default constructor [operate Xml without namespaces]

Public XmlHelper (bool forHtml) // when the parameter is true, it indicates the Html operation, and false indicates the xml operation.

Public XmlHelper (string nameSpaceUrl) // Xml for operating the namespace

 

2: Xml/Html loading:

Method prototype:

Public void LoadXml (string xml) // load from a string

Public bool Load (string absFilePath) // Load from the file path

Public bool Load (string absFilePath, XmlCacheLevel level) // set the xml/html cache level

Sample Code:

XmlHelperDoc = new XmlHelper(True); // operate html
If (doc. Load (Server. MapPath ("html/index.html"), XmlCacheLevel. Hour ))
{
// Html loaded successfully and cached for 1 hour
}

 

3: Query

 

3.1: Single-node query:

Method prototype:

Public XmlNode GetByID (string id) // find the node by ID
Public XmlNode GetByID (string id, XmlNode parentNode) // locate the node by ID under the parentNode Node
Public XmlNode GetByName (string name) // locate a node by Name
Public XmlNode GetByName (string name, XmlNode parentNode ))

Public XmlNode Get (string tag, string attr, string value, XmlNode parentNode) // search for a node based on the tag, attribute, attribute value, and whether to include the parent node.

Example:

Xml sample node:

<P id = "pID"> pID </p> <div id = "divID"> <p id = "pID"> another pID </p> </div>

XmlNode node = doc. GetByID ("pID", doc. GetByID ("divID"); // obtain the ID node

 

3.2: List node query:

Method prototype:

Public XmlNodeList GetList (string tag)
Public XmlNodeList GetList (string tag, XmlNode parentNode)
Public XmlNodeList GetList (string tag, string attr)
Public XmlNodeList GetList (string tag, string attr, XmlNode parentNode)
Public XmlNodeList GetList (string tag, string attr, string value)
Public XmlNodeList GetList (string tag, string attr, string value, XmlNode parentNode)

Example:

Xml sample node:

<P id = "pID"> pID </p> <div id = "divID"> <p id = "pID"> another pID </p> </div>

XmlNodeList nodeList = doc. GetList ("p", "id", "pid"); // get all p nodes

 

4. Create a node:

Method prototype:

Public XmlNode CreateNode (string tag, string text, params string [] attrAndValue)

Public void CreateNodeTo (XmlNode parentNode, string tag, string text, params string [] attrAndValue)

Example:

Xml sample node:

Start node: <div id = "divID"> </div>

Code creation: doc. CreateNodeTo (doc. GetByID ("divID"), "p", "pID", "id", "pID ");

After creation, it becomes: <div id = "divID"> <p id = "pID"> another pID </p> </div>

 

5. add nodes:

Method prototype:

Public void AppendNode (XmlNode parentNode, XmlNode childNode)

Public void AppendNode (XmlNode parentNode, XmlNode childNode, int position) // position is to put childNode after the nth subnode of parentNode

 

6. delete a node:

Method prototype:

Public void Clear (XmlNode node) // retains the node and only clears the content and attributes.
Public void Remove (XmlNode node) // delete a node
Public void Remove (string id) // delete a node based on the node ID
Public void RemoveChild (string id, int index) // remove the nth subnode under an ID Node
Public void RemoveChild (XmlNode node, int index) // remove the nth subnode under a node
Public void RemoveAttrList (params string [] attrNames) // remove the specified attribute of the entire document

 

7: node interaction | replace | insert [Before and After], [supports operations in multiple different Document nodes]

Method prototype:

Public void InterChange (XmlNode xNodeFirst, XmlNode xNodeLast) // two-node InterChange location

Public void ReplaceNode (XmlNode newNode, XmlNode oldNode) // node replacement

Public void InsertAfter (XmlNode newNode, XmlNode refNode) // insert newNode to refNode

Public void InsertBefore (XmlNode newNode, XmlNode refNode) // insert newNode before refNode

 

8: node judgment:

Method prototype:

Public bool Contains (string id) // whether an ID node exists

Public bool Contains (string id, XmlNode parentNode) // whether an ID node exists under parentNode

 

9: attribute operations

Method prototype:

Public bool HasAttr (string nodeID, string attrName) // check whether a node has an attribute.

Public bool HasAttr (XmlNode node, string attrName)

Public string GetAttrValue (string nodeID, string attrName, params string [] defaultValue) // gets the attribute value of a node and allows you to set the default value.

Public string GetAttrValue (XmlNode node, string attrName, params string [] defaultValue)

Public void RemoveAttr (string nodeID, params string [] attrNames) // remove a specified attribute from a node

Public void RemoveAttr (XmlNode node, params string [] attrNames)

 

10: Save As xml/html

Method prototype:

Public void Save () // Save and replace the original file

Public void Save (string fileName) // Save it to the specified file path

 

3. Interaction with CYQ. Data. Table

 

1: Native value assignment operation

Method prototype:

Public void Set (string id, string value) // assign a value to the InnerXml attribute of the ID Node
Public void Set (string id, SetType setType, params string [] values) // assign values to the setType [different attributes] of the ID node. If it is A link, multiple values can be assigned.
Public void Set (XmlNode node, SetType setType, params string [] values)

Example:

Original: <a id = "aID"> This is an original A link </id>

Doc. Set ("aID", SetType. A, "This is A link", "http://www.cyqdata.com % 22, % 22_blank /");

Results: <a id = "aID" href = http://www.cyqdata.com/target = "_ blank"> This is A link </id>

 

2: interaction with MDataRow [ID = any three letter prefixes + field names]

Method prototype:

Public void LoadData (MDataRow row) // load row data
Public void SetFor (string id) // assign the row data to the InnerXml attribute of the ID Node
Public void SetFor (string id, SetType setType) // assign the row data to the specified attribute of the ID node.
Public void SetFor (string id, SetType setType, params string [] values) // The last parameter allows formatting of values.

Example:

Original <span id = "labHits"> click </span>

Doc. SetFor ("labHits", SetType. InnerText, ValueReplace. Source + "(" + ValueReplace. New + ")");

Result: <span id = "labhits"> click (12) </span>

ValueReplace. Source: the attribute value of the original node.

ValueReplace. New: indicates the database value.

 

3: interaction with MDataTable

Method prototype:

Public void LoadData (MDataTable table) // load the table

// SetForeach copies and loops the ID Node
Public void SetForeach (string id, SetType setType, params object [] formatValues)
Public void SetForeach (string id, string text, params object [] formatValues)
Public delegate string SetForeachEventHandler (string text, object [] values, int row );
Public event SetForeachEventHandler OnForeach; // format each line

 

Example 1: SetForeach (string id, string text, params object [] formatValues)

Original Tag: <select id = "selClassID"> </select> doc. LoadData (table); // load table
Doc. SetForeach ("selClassID", "<option value =" {0} ">{1} </option>", Class. ID, IsUserLang? Class. name: Class. PKey); Result Tag: <select id = "selClassID"> <option value = "1"> id 1 </option> .... </select>

This statement loops out a drop-down list.

 

Example 2: SetForeach (string id, SetType setType, params object [] formatValues)

Original Tag: <select id = "selClassID"> <option value = "{0}" >{1} </option> </select>

Doc. SetForeach ("selClassID", SetType. InnerXml, Class. ID, IsUserLang? Class. Name: Class. PKey );

Result Tag: Same as above.

 

Example 3: Circular formatting:

Doc. OnForeach + = new XmlHelper. SetForeachEventHandler (Document_OnForeach );

String Document_OnForeach (string text, object [] values, int row)
{

// Text: <option value = "{0}" >{1} </option>
Values [0] = "2 ";

Values [1] = "ID 2 ";

Return text;
}

Result Tag: <select id = "selClassID"> <option value = "2"> id 2 </option>... </select>

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.