Use c # To operate the processing commands (PI) in XML)

Source: Internet
Author: User
Tags processing instruction xml parser

What is a processing command? Processing Instructions (PI) uses "<? ?>" A tag that describes information about a specific application. Xml documents can contain multiple processing commands for different applications. The Processing Command consists of two parts: target and value. The role of target is similar to a "name", and the string followed by target is value. value can contain multiple tags.
 

<? Target value?>

Is the Xml declaration a processing instruction? The answer is yes. Xml declaration is a special processing instruction. It is special because its value format is pre-defined. Another common example of processing commands is an external style sheet. The style sheet processing command also has a pre-defined format value, which consists of some pseudo attributes. Why is it "pseudo attribute"? This is because its value looks like a few Normal Xml attributes, but in fact they are just a string.
 

<? Xml-stylesheet href = "standardstyle.css" title = "Standard Stylesheet" type = "text/css"?>

However, the format of the value of the Processing Command is open. The processing command is not a part of the document data. The Xml parser does not handle its content, but directly transmits it to the client application.

In Microsoft Office suite, InfoPath uses processing commands to indicate whether the Xml file can be viewed on the InfoPath client.
 

<? Mso-application progid = "InfoPath. Document"?>

Another Processing Command, mso-infoPathSolution, tells the location of the InfoPath solution template. The template contains the layout conversion information of the layout Xml file, and the structure information of the view is already the data source information.

In the c # application, how do I operate Xml documents containing processing commands?

How can we process and read the existing processing commands in the Xml document? First, the processing commands can be selected like other nodes in the document. XPath uses the predicate processing-instruction () to test whether the node is a processing instruction.

In the System. XML namespace, there is an XmlProcessingInstruction class. After you select a node from the Xml document, you can convert the returned XMLNode object to this type. This type provides a friendly interface to operate on the value of the Processing Command. To read its value, you only need to access the Value Attribute of the object.

To change the Value of the Processing Instruction, you only need to assign the new Value to the Value attribute of the object.

To add a new processing instruction to the Xml document, you can use the CreateProcessingInstruction method of the XMLDocument class. Then, use the InsertBefore or InsertAfter method to add the XmlProcessingInstruction object to the Xml document.

To delete an existing processing instruction from the Xml document, first select the XmlNode object of the processing instruction, but do not need to convert it to the XMLProcessingInstruction object. The RemoveChild method of the XMLDocument class can be used to delete the object.

The following code contains all the content mentioned in this article:
 

// Xml file path
String strPath = "path"

// Load Xml document content
XmlDocument doc = new XmlDocument ();
Doc. XmlResolver = null;
Doc. Load (strPath );

// Display Xml document content
Console. WriteLine (doc. InnerXml. ToString (). Replace ("> <", "> <"));
// Process instructions for reading Xml documents
XmlProcessingInstruction pi = (XmlProcessingInstruction) doc. SelectSingleNode ("/processing-instruction (" mso-infoPathSolution ")");
// Display the value of the Processing Instruction
Console. WriteLine (pi. Value );

// Update the value of the Processing Instruction
Pi. Value = "updated value ";

// Display the updated processing command value
Console. WriteLine (pi. Value );

// Create a New Processing Instruction
XmlProcessingInstruction piNew = doc. CreateProcessingInstruction ("new-pi", "my new processing instruction ");

// Add the Processing Instruction to the document
Doc. InsertBefore (piNew, doc. ChildNodes [3]);

// Delete the Processing Instruction
XmlNode ndDel
Related Article

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.