Options on an XML Document
Options on an XML Document
Comments
Overview of Types of Nodes
To differentiate the various nodes that belong to an XML document, they are classified by their category. As mentioned already, the types of nodes are listed in the XmlNodeType enumeration.
Manually Creating a Comment
A comment is a character, a line or a paragraph that is not considered as part of the XML code that needs to be processed. A comment allows you to insert notes or personal observations in an XML document. For this reason, a commented section can be written any way you like. This means that a comment can include plain text, formulas, expressions, or even XML code as long as you know that that XML code will not be validated: it will be ignored by the XML parser.
To create a comment in an XML document, you use the following formula:
<!-- Blah Blah Blah ->
Between <!-- and -->, any text in that section is considered a comment and you can include anything you want. Both sections of the comment use two dashes, not more, not less. Here is an example:
<?xml version="1.0" encoding="utf-8"?>
<!-- In this collection, we will keep each title "as is" -->
<videos>
<video>
<title>The Distinguished Gentleman</title>
<director>Jonathan Lynn</director>
<length>112 Minutes</length>
<format>DVD</format>
<rating>R</rating>
</video>
<video>
<title>Her Alibi</title>
<director>Bruce Beresford</director>
<length>94 Mins</length>
<format>DVD</format>
<rating>PG-13</rating>
</video>
</videos>
This would produce:
Characteristics of a Comment
Tsupport comments in an XML document, the System.Xml presents a class named XmlComment:
public class XmlComment : System.Xml.XmlCharacterData
As a result, an XML comment is a regular object created from a class. Such an object has properties and methods. Like every XML object, a comment has a name. To support this, the XmlComment class has a read-only property named Name:
public override string Name { get; }
This property allows you to retrieve the name of a comment that is included in the document.
Programmatically Creating a Comment
Consider an XML document as follows:
<?xml version="1.0" encoding="utf-8"?> <musical-instruments introduction="Solo Music Store"> <store-item> <item-number>482758</item-number> <item-name pickup-configuration="Piezo" number-of-strings="6">Fender Malibu Player Acoustic Guitar</item-name> <price>375.48</price> </store-item> <store-item> <item-number level="Introductory" items-included="1">820750</item-number> <category>Keyboards</category> <sub-category>Synthesizers</sub-category> <item-name number-of-keys="61">Roland JUNO-DS 61-Key Lightweight Synth-Action Keyboard</item-name> <price discount="20">724.65</price> </store-item> <store-item> <item-number>925374</item-number> <item-name color="Black" number-of-strings="5" hand-orientation="Right" pickup-configuration="S" neck-material-type="Maple" fretboard-material-type="Rosewood">Yamaha BB235 BB-Series 5-String Bass Guitar</item-name> <price discount="20">372.68</price> </store-item> </musical-instruments>
To let you create a comment, they XmlComment class has a method named CreateComment. Its syntax is:
public virtual XmlComment CreateComment(string data);
This method takes as argument the text that would go into the commented section. After calling it, if the method succeeds, which it usually does, it returns the XmlComment object that was created. Here is an example that creates a comment:
using System.Xml;
namespace SoloMusicStore
{
public partial class Management : Form
{
public Management()
{
InitializeComponent();
}
private void Management_Load(object sender, EventArgs e)
{
var xdStoreItems = new XmlDocument();
string strStoreItems = "C:\\Solo Music Store\\StoreItems.xml";
xdStoreItems.Load(strStoreItems);
XmlNodeList xnlItemsNumbers = xdStoreItems.GetElementsByTagName("item-number");
for (int i = 0; i < xnlItemsNumbers.Count; i++)
{
if (xnlItemsNumbers[i]!.InnerText.Equals("820750"))
{
// Add a comment
string strComment = "This is a good synthesizer that can be used by students and professionals.";
XmlComment xcDescription = xdStoreItems.CreateComment(strComment);
xnlItemsNumbers[i]!.ParentNode!.PrependChild(xcDescription);
xdStoreItems.Save(strStoreItems);
break;
}
}
}
}
}
This would produce:
<?xml version="1.0" encoding="utf-8"?>
<musical-instruments introduction="Solo Music Store">
<store-item>
<item-number>482758</item-number>
<item-name pickup-configuration="Piezo"
number-of-strings="6">Fender Malibu Player Acoustic Guitar</item-name>
<price>375.48</price>
</store-item>
<store-item>
<!--This is a good synthesizer that can be used by students and professionals.-->
<item-number level="Introductory"
items-included="1">820750</item-number>
<category>Keyboards</category>
<sub-category>Synthesizers</sub-category>
<item-name number-of-keys="61">Roland JUNO-DS 61-Key Lightweight Synth-Action Keyboard</item-name>
<price discount="20">724.65</price>
</store-item>
<store-item>
<item-number>925374</item-number>
<item-name color="Black"
number-of-strings="5"
hand-orientation="Right"
pickup-configuration="S"
neck-material-type="Maple"
fretboard-material-type="Rosewood">Yamaha BB235 BB-Series 5-String Bass Guitar</item-name>
<price discount="20">372.68</price>
</store-item>
</musical-instruments>
Introduction
Except for comments, the parser is used to "scan" the whole XML file to analyze it. Every tag is then interpreted. As we mentioned already, the value of each tag can be displayed in a browser between its opening and its closing tag, and the browser uses different font styles to make a distinction. When creating some tags and some sections of the file, you may want the parser to consider those particular tags and sections as regular text. That is, you may want the parser to treat a certain tag and its value as if it were regular text even though it is created as an XML file.
Manually Creating a CDATA Section
To prevent the parser from interpreting a tag regularly but to treat that tag and its value as regular text, you can create it in a CDATA section. To do this, create a section that starts with <![CDATA[, followed by anything you want, and ending with ]]>. The formula used is:
<![CDATA[ Blah Blah Blah ]]>
Between <![CDATA[ and ]]>, you can type anything, including one or more normal XML tags. Here is an example:
<?xml version="1.0" encoding="utf-8"?> <!-- In this collection, we will keep each title "as is" --> <videos> <![CDATA[<VdoColl>Collection of Videos</VdoColl>]]> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> <length>112 Minutes</length> <format>DVD</format> <rating>R</rating> </video> <video> <title>Her Alibi</title> <director>Bruce Beresford</director> <length>94 Mins</length> <format>DVD</format> <rating>PG-13</rating> </video> </videos>
Programmatically Creating a CDATA Section
The .NET Framework supports the creation of a CDATA section through a class named XmlCDataSection. This class is equipped with a property named Name. This property allows you to get the name of a CDATA section in an XmlDocument object.
To let you programmatically create a CDATA section, the XmlDocument class is equipped with a method named CreateCDataSection. Its syntax is:
public virtual XmlCDataSection CreateCDataSection(string data);
|
|||
Previous | Copyright © 2005-2023, FunctionX | Monday 06 December 2021 | Next |
|