LINQ to XML techniques: adding a processing instruction

In this post we saw how to add a declaration to an XML document. A well-formatted XML document starts with a declaration whose main function is to declare formally that the upcoming document is of the XML type. The XDeclaration object helps us to easily add a declaration to an XML document. Note that the XDocument.ToString method does not print the declaration for some reason so we need to print it separately if needed.

In this post we’ll see how to add a processing instruction to an XML document.

A processing instruction declares an action that should be carried out on the document. These instructions do not add to the contents of the document, i.e. they do not belong to the actual information conveyed by the document. A good example is when a stylesheet is added to render the document in some way. However, a processing instruction is not something that XML processing software will automatically understand and act upon. It won’t raise an exception if there’s anything wrong with an instruction since there are no pre-defined nodes and attributes attached to an instruction. If your code encounters an instruction then you’ll need to know what it means and what to do with it otherwise it will go unnoticed.

As a result, processing instructions are rarely added to XML documents in reality.

The XProcessingInstruction represents a processing instruction. Let’s see how to add an XML stylesheet reference and some fancy comments to an XML document. Note that these instructions can be added at various levels of the document:

XDocument bandsDocument = new XDocument(
	new XProcessingInstruction("xml-stylesheet", "mystyle.css"),
	new XElement("Bands",
		new XElement("Band",
			new XAttribute("genre", "rock"),
			new XElement("Name", "Queen"),
			new XElement("NumberOfMembers", 4)),
		new XElement("Band",
			new XAttribute("genre", "progressive"),
			new XElement("Name", "Genesis"),
			new XElement("NumberOfMembers", 5)),
		new XElement("Band",
			new XProcessingInstruction("comment", "this-is-a-comment"),
			new XAttribute("genre", "metal"),
			new XElement("Name", "Metallica"),
			new XElement("NumberOfMembers", 4))));

The above code generates the following document:

<?xml-stylesheet mystyle.css?>
<Bands>
  <Band genre="rock">
    <Name>Queen</Name>
    <NumberOfMembers>4</NumberOfMembers>
  </Band>
  <Band genre="progressive">
    <Name>Genesis</Name>
    <NumberOfMembers>5</NumberOfMembers>
  </Band>
  <Band genre="metal">
    <?comment this-is-a-comment?>
    <Name>Metallica</Name>
    <NumberOfMembers>4</NumberOfMembers>
  </Band>
</Bands>

Processing instructions can be added to an XDocument after it has been initialised using the various Add methods. Here we add the instruction as the first node to the document:

XDocument bandsDocument2 = new XDocument(
	new XElement("Bands",
		new XElement("Band",
			new XAttribute("genre", "rock"),
			new XElement("Name", "Queen"),
			new XElement("NumberOfMembers", 4)),
		new XElement("Band",
			new XAttribute("genre", "progressive"),
			new XElement("Name", "Genesis"),
			new XElement("NumberOfMembers", 5)),
		new XElement("Band",
			new XAttribute("genre", "metal"),
			new XElement("Name", "Metallica"),
			new XElement("NumberOfMembers", 4))));
bandsDocument2.AddFirst(new XProcessingInstruction("comment", "this-is-some-comment"));

You can view all LINQ-related posts on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: