What is a processing instruction? The processing instruction (processing INSTRUCTIONS,PI) is a label that is surrounded by "?>" to describe specific application information. An XML document can contain multiple processing instructions for different applications. The processing instruction consists of two parts, target and value. Target's role is similar to "name", and the string after target is Value,value can contain multiple tags.
<?target value?>
So is the XML declaration a processing instruction? The answer is yes, the XML declaration is a special processing instruction, and it is special because its value format is predefined. Another common example of a processing instruction is an external style sheet. The stylesheet processing instruction also has a predefined format value value, which consists of a number of pseudo attributes. Why is it called "pseudo attribute" because its value looks like several 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 processing instruction value value is open. The processing instruction is not part of the document data, and the XML parser ignores its contents and passes it directly to the client application.
InfoPath in the Microsoft Office suite uses processing instructions to indicate whether an XML file can be viewed with an InfoPath client.
<?mso-application progid= "Infopath.document"?>
Another processing instruction, Mso-infopathsolution, tells the location of the InfoPath solution template. The template contains layout transformation information for layout XML files, schema information for the view, and data source information.
So how do you manipulate an XML document that contains processing instructions in a C # application?
How do we process and read the existing processing instructions in an XML document? First, the processing instruction can be selected as the other nodes in the document. XPath uses the predicate (predicate) processing-instruction () to test whether the node is a processing instruction.
In the System.Xml namespace, there is a xmlprocessinginstruction class. Once you have selected a node from an XML document, you can convert the returned XmlNode object to this type, which provides a very friendly interface for manipulating the value of the processing instruction. To read its value, you only need to access the Value property of the object.
To change the value of a processing instruction, you only need to assign the new value to the object's Values property.
To add a new processing instruction to an 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 remove an existing processing instruction from an XML document, first select the XmlNode object for the processing instruction, but do not need to convert it to a XmlProcessingInstruction object. You can then delete it by using the RemoveChild method of the XmlDocument class.
The following code contains all of the things that are 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 Contents
Console.WriteLine (Doc. Innerxml.tostring (). Replace ("><", ">\r\n\n<");
//Read processing instruction for XML document
xmlprocessinginstruction pi = (xmlprocessinginstruction) doc. selectSingleNode ("/processing-instruction" ("mso-infopathsolution\"));
//Displays the value of the processing instruction Console.WriteLine (pi. Value); The value
Pi of the
//update processing instruction. Value = "Updated value";
//Displays the updated processing instruction value
Console.WriteLine (pi. Value);
//Create a new processing instruction
XmlProcessingInstruction pinew = doc. Createprocessinginstruction ("New-pi", "my New processing instruction");
//Add processing instructions to the document
Doc. InsertBefore (Pinew, Doc. CHILDNODES[3]);
//delete processing instruction
XmlNode Nddel = doc. selectSingleNode ("/processing-instruction" ("mso-application\"));
Doc. RemoveChild (Nddel);
//Displays the changed XML document
Console.WriteLine (Doc. Innerxml.toString (). Replace ("><", ">\r\n\n<"));