Working with XML Processing Instructions in C#
 
Published: 09 Sep 2004
Unedited - Community Contributed
Abstract
Recently I had to work with XML Processing Instructions; adding them to an XML Document or editing the processing instructions value. This will demonstrate how to do this using C#.
by Michelle Beall
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33878/ 56

Background

[ Download Code ]

Firstly, what is an XML processing instruction? A processing instruction (PI) is a tag that encodes specific application information which begins with <? and ends with ?>.  An XML document can contain multiple processing instructions for different applications.  There are two parts to a processing instruction, a target and a value.  The target acts like a name and the value is the string after the target.  The value can consist of multiple tokens.

<?target value?>

What about the XML declaration, is it a processing instruction?  Yes, it is a special processing instruction.  It is a special kind of processing instruction that has a defined format for its value.  Another common example of a processing instruction is for adding stylesheets.  The stylesheet processing instruction also has a defined format for its value, which contains several pseudo-attributes.  Pseudo because they act like a normal element's attributes.

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

Otherwise the format of a processing instruction's value is open.  Processing instructions are not part of the document's data, rather are more like comments and are overlooked by the XML parser which simply passes them on to the client application.

Microsoft's application InfoPath uses processing instructions to indicate that the XML file should be viewed within the InfoPath client. 

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

A second processing instruction, mso-infoPathSolution, tells InfoPath where the location of the solution template is located.  The template contains information on the transformation for the layout, any and all views as well as the expected schema of the XML file and any data sources.

So how do we work with an XML document's processing instructions from within a C# application?

Accessing Processing Instructions

[ Download Code ]

So how do we access and read an existing processing instruction from an XML document?  Processing instructions can be selected just like any other node from a document.  The XPath uses a predicate processing-instruction() to test that the node is indeed a processing instruction. 

In the System.XML namespace there is a class called XmlProcessingInstruction.  When you select a single node from the XML document you can cast the returning XMLNode object to this type which will give you a nice interface to access the processing instruction's value.  To read the value, simply select the object's Value member.

To change the value of the processing instruction, simply assign the new string value to the object's Value member.

To add a new processing instruction to the XML document, use the CreateProcessingInstruction method of the XMLDocument class.  Then add the XmlProcessingInstruction object to the XML Document by using either the InsertBefore or InsertAfter method.

To delete an existing processing instruction from an XML Document, select the processing instruction node as before, but this time do not cast it to an XMLProcessingInstruction object.  Instead keep it as type XmlNode.  Then use the RemoveChild method of the XMLDocument class.

Everything is demonstrated in the following code.

// Get physical path of the XML document
string strPath = Server.MapPath("Sample.xml");  

// Load the contents of the XML document
XmlDocument doc = new XmlDocument();
doc.XmlResolver = null;
doc.Load(strPath);  

// display contents of original Sample.xml
txtSample.Text = doc.InnerXml.ToString().Replace("><", ">\r\n\n<");  

// read processing instruction from document
XmlProcessingInstruction pi = (XmlProcessingInstruction)doc.SelectSingleNode
                                  ("/processing-instruction(\"mso-infoPathSolution\")");  
// display processing instruction value
txtMsoInfoPathSolution.Text = pi.Value;  

// update processing instruction value
pi.Value = "updated value"// display updated processing instruction value
txtMsoInfoPathSolutionUpdated.Text = pi.Value; 

// create a new processing instruction
XmlProcessingInstruction piNew = doc.CreateProcessingInstruction
                                  ("new-pi", "my new processing instruction");  

// add processing instruction to the document
doc.InsertBefore(piNew, doc.ChildNodes[3]); 

// delete a processing instruction
XmlNode ndDel = doc.SelectSingleNode("/processing-instruction(\"mso-application\")");
doc.RemoveChild(ndDel);  

// display in memory changes of Sample.xml to 
// show updated and added processing instructions
txtSampleUpdated.Text = doc.InnerXml.ToString().Replace("><", ">\r\n\n<");
Review

[ Download Code ]

The image (Figure 1) below shows the changes that the code sample made to an existing xml document.

Figure 1
Figure 1

XML processing instructions have their uses and being able to manipulate them will hopefully be a nice addition to your coding toolbox.



User Comments

Title: re:   
Name: spam filter
Date: 2005-02-17 6:47:20 AM
Comment:
can you help me with an example in asp.net with c# to extract data from XML file and to display into table?
Thank you in advance.
Title: RE: Working with XML Processing Instructions in C#   
Name: Michelle
Date: 2004-11-21 11:42:16 AM
Comment:
Thanks for the great question!

Microsoft InfoPath uses processing instructions to tie the xml data generated by filling out a form to a specific InfoPath template (xsn) as well as to indicate that the xml should be rendered using the InfoPath Application. There are several cases where manipulating the PI would be useful, such as removing those PIs that bind the xml to InfoPath or to add your own PIs to bind to your own xml rendering solution. Another example where I've used PIs is to add information to an XML document without having to incorporate it into the data schema, such as default save location URL for saving the file to an alternative library in Sharepoint. So you can see there are lots of applications of using and manipulating PIs.
Title: Working with XML Processing Instructions in C#   
Name: Pankaja Shankar
Date: 2004-11-20 3:17:38 PM
Comment:
I was wondering when will this be of use. Could you please illustrate and explain a bit more in your response to my comment?

Thanks.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 3:35:48 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search