To create a comment, 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:
To manually include a comment in your XML loading code, use
the same formula seen above. Here are examples:
Imports System.Xml
Imports System.IO
Public Class Exercise
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnCreate.Click
Dim docVideo As XmlDocument = New XmlDocument
docVideo.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<!-- Personal Video Collection -->" & _
"<videos>" & _
"<!-- Each video is defined with various pieces of information -->" & _
"<!-- such as the the title and the ISBN, etc" & _
"<!-- We start representing a video with a video node -->" & _
"<!-- Most videos have an ISBN, which is an international -->" & _
"<!-- number that uniquely identifies each (released) video. -->" & _
"<!-- The title is the official title of the video. -->" & _
"<!-- We omitted the small titles/subscript. -->" & _
"<!-- The length is provided in minutes. -->" & _
" <video>" & _
" <ISBN>0-7888-1623-3</ISBN>" & _
" <title Screenplay=""Marty Kaplan"">" & _
" The Distinguished Gentleman</title>" & _
" <director>Jonathan Lynn</director>" & _
" <length>112</length>" & _
" <format>DVD</format>" & _
" <rating>R</rating></video>" & _
" <video>" & _
" <ISBN>0-7907-3900-3</ISBN>" & _
" <title WrittenBy=""Charlie Peter"">" & _
" Her Alibi</title>" & _
" <director>Bruce Beresford</director>" & _
" <length>94</length>" & _
" <format>DVD</format>" & _
" <rating>PG-13</rating>" & _
" </video>" & _
"</videos>")
docVideo.Save("videos.xml")
End Sub
End Class
This would produce:
Programmatically Creating a Comment |
|
You can programmatically create a comment. This makes it
possible to let the user specify what text to include as a command, and then
create that commented section. The System.Xml represents a comment through the XmlComment
class. Like any other part of an XML file, a comment is represented by the XmlComment.Name
property. This allows you to retrieve the name of a comment that is included in
the document.
To create a comment, you can call the XmlDocument.CreateComment()
method. Its syntax is:
Public Overridable Function CreateComment(data As String) As XmlComment
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. To
create a comment, you can add it before the node you want to apply to it. Here
are examples:
Imports System.Xml
Imports System.IO
Public Class Exercise
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnCreate.Click
Dim Filename As String = "videos.xml"
Dim Editor As VideoEditor = New VideoEditor
Dim DOMVideo As XmlDocument = New XmlDocument
Dim ISBN As String, Title As String
Dim Director As String, Length As Integer
Dim Format As String, Rating As String
' If a video named videos.xml was created already ...
If File.Exists(Filename) Then
' ... then you will open it
Else ' If there is no such a video, then create it
DOMVideo.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<!-- Personal Video Collection -->" & _
"<videos></videos>")
DOMVideo.Save("videos.xml")
End If
' Display the Video Editor dialog box and find out
' whether the user clicked the submit button after using it
If Editor.ShowDialog = Windows.Forms.DialogResult.OK Then
' If the user clicked Submit, get the values from the dialog box
ISBN = Editor.txtISBN.Text
Title = Editor.txtTitle.Text
Director = Editor.txtDirector.Text
Length = CInt(Editor.txtLength.Text)
Format = Editor.cbxFormats.Text
Rating = Editor.cbxRatings.Text
' Open the videos.xml file
DOMVideo.Load(Filename)
' Get a reference to the root element
Dim RootElement As XmlElement
RootElement = DOMVideo.DocumentElement
' Create the title element
Dim VideoParent As XmlElement = DOMVideo.CreateElement("video")
RootElement.AppendChild(VideoParent)
' Create a comment for the ISBN
Dim strComment As String = "ISBN is an international number used " & _
"to uniquely identify each (released) video"
Dim CommentElement As XmlComment = DOMVideo.CreateComment(strComment)
VideoParent.AppendChild(CommentElement)
' Create the ISBN element
Dim ElementISBN As XmlElement = DOMVideo.CreateElement("ISBN")
Dim TextISBN As XmlText = DOMVideo.CreateTextNode(ISBN)
VideoParent.AppendChild(ElementISBN)
VideoParent.LastChild.AppendChild(TextISBN)
' Create the title element
Dim ElementTitle As XmlElement = DOMVideo.CreateElement("title")
Dim TextTitle As XmlText = DOMVideo.CreateTextNode(Title)
VideoParent.AppendChild(ElementTitle)
VideoParent.LastChild.AppendChild(TextTitle)
' Create the ISBN element
Dim ElementDirector As XmlElement = DOMVideo.CreateElement("director")
Dim TextDirector As XmlText = DOMVideo.CreateTextNode(Director)
VideoParent.AppendChild(ElementDirector)
VideoParent.LastChild.AppendChild(TextDirector)
' Create a comment for the length
strComment = "The length of a video is in minutes"
CommentElement = DOMVideo.CreateComment(strComment)
VideoParent.AppendChild(CommentElement)
' Create the title element
Dim ElementLength As XmlElement = DOMVideo.CreateElement("length")
Dim TextLength As XmlText = DOMVideo.CreateTextNode(Length)
VideoParent.AppendChild(ElementLength)
VideoParent.LastChild.AppendChild(TextLength)
' Create a comment for the length
strComment = "The available formats are: VHS, DVD, and Blue Ray"
CommentElement = DOMVideo.CreateComment(strComment)
VideoParent.AppendChild(CommentElement)
' Create the format element
Dim ElementFormat As XmlElement = DOMVideo.CreateElement("format")
Dim TextFormat As XmlText = DOMVideo.CreateTextNode(Format)
VideoParent.AppendChild(ElementFormat)
VideoParent.LastChild.AppendChild(TextFormat)
' Create a comment for the length
strComment = "The ratings can be G, PG, PG-13, R, N/R, or Unrated"
CommentElement = DOMVideo.CreateComment(strComment)
VideoParent.AppendChild(CommentElement)
' Create the rating element
Dim ElementRating As XmlElement = DOMVideo.CreateElement("rating")
Dim TextRating As XmlText = DOMVideo.CreateTextNode(Rating)
VideoParent.AppendChild(ElementRating)
VideoParent.LastChild.AppendChild(TextRating)
DOMVideo.Save("videos.xml")
End If
End Sub
End Class
This would produce:
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 |
|
If you already know the whole content of the XML document
you want to create, you can create the CDATA section and include it in the
XmlDocument.LoadXml() method. Here is an example:
Imports System.Xml
Imports System.IO
Public Class Exercise
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnCreate.Click
Dim docVideo As XmlDocument = New XmlDocument
docVideo.LoadXml("<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<!-- Personal Video Collection -->" & _
"<videos>" & _
"<!-- A typical video of our collection appears as follows: -->" & _
"<![CDATA[" & _
"<video> " & _
" <ISBN>0--0000-0000-0</ISBN>" & _
" <title>The Video's Title</title>" & _
" <director>The person(s) who directed the video</director>" & _
" <length>The length in minutes</length>" & _
" <format>VHS, DVD, or Blue Ray</format>" & _
" <rating>G, PG, PG-13, R, Unrated, Other</rating>" & _
"</video> ]]>" & _
" <video>" & _
" <ISBN>0-7888-1623-3</ISBN>" & _
" <title Screenplay=""Marty Kaplan"">" & _
" The Distinguished Gentleman</title>" & _
" <director>Jonathan Lynn</director>" & _
" <length>112</length>" & _
" <format>DVD</format>" & _
" <rating>R</rating></video>" & _
" <video>" & _
" <ISBN>0-7907-3900-3</ISBN>" & _
" <title WrittenBy=""Charlie Peter"">" & _
" Her Alibi</title>" & _
" <director>Bruce Beresford</director>" & _
" <length>94</length>" & _
" <format>DVD</format>" & _
" <rating>PG-13</rating>" & _
" </video>" & _
"</videos>")
docVideo.Save("videos.xml")
End Sub
End Class
You can create a CDATA section
at the time you are adding an element to your XML document. The .NET Framework supports the creation of a CDATA
section through the XmlCDataSection class. This class is equipped with a
Name property that allows you t retrieve the name of a CDATA section in
an XmlDocument object.
To programmatically create a CDATA section, you can
call the XmlDocument.CreateCDataSection() method. Its syntax is:
Public Overridable Function CreateCDataSection ( _
data As String _
) As XmlCDataSection
This method receives the content of the CDATA section
as argument. If the method succeeds, which it usually does, it returns an XmlCDataSection
value.
|