Microsoft KB Archive/308060

= HOW TO: Locate and Replace Special Characters in an XML File with Visual Basic .NET =

Article ID: 308060

Article Last Modified on 9/3/2003

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q308060





For a Microsoft Visual C# .NET version of this article, see 316063.



For a Microsoft Visual Basic 6.0 version of this article, see 251354.

IN THIS TASK
SUMMARY Description of the Technique Determine Whether You Must Replace a Special Character
 * Not Required: An XML File with Special Characters
 * Required: An XML File with Special Characters

Replace the Special Characters
 * Create the XML File
 * Create a Visual Basic .NET Project

REFERENCES



SUMMARY
This article describes how to replace special characters in an Extensible Markup Language (XML) file by using Visual Basic .NET.

back to the top

Description of the Technique
XML predefines the following five entity references for special characters that would otherwise be interpreted as part of markup language:

You can use entity and character references to escape the left angle bracket, the ampersand, and other delimiters. You can also use numeric character references; they are expanded immediately when they are recognized and they are treated as character data, so you can use the numeric character references

If you are declaring either of the following two entities
 * lt
 * amp

you have to declare them as internal entities whose replacement text is a character reference to the respective character (the left angle bracket or the ampersand) that is being escaped; the double escaping is required for these entities so that references to them produce a well-formed result.

If you are declaring any of the following three entities
 * gt
 * apos
 * quot

you have to declare them as internal entities whose replacement text is the single character being escaped.

back to the top

Not Required: XML Files in Which the Data Is Retrieved from a Database
When you are using the Microsoft .NET Framework, data is retrieved and stored in a DataSet. When you are writing data from a DataSet to an XML file by using the WriteXml method, the special characters that are referred to in the &quot;Summary&quot; section are replaced with the respective character references; therefore, when you are writing XML files and you are using a DataSet, no special replacement process is required.

back to the top

Required: An XML File with Special Characters
Sometimes the XML file or the XML data that is coming from a third party may use these special characters; in this scenario, the data generates errors when you load it into an XmlDocument object or an XmlReader object.

The following error is generated when the ampersand character is encountered:

An Error occurred while parsing, line  , position.

where line  and position   represent the exact position of the special character.

The following error occurs when a left angle bracket is encountered:

The '<' character, hexadecimal value 0x3C,cannot be included in a name. Line, position.

In this error message, the line  and position   do not indicate the position where the left angle bracket exists, but where the second left angle bracket is encountered.

If the XML file contains a right angle bracket (>), a straight quotation mark (&quot;) or an apostrophe ('), these are handled by the XmlReader and the XmlDocument objects because only single character replacement is required for these characters.

back to the top

Replace the Special Characters
To replace the ampersand and the left angle bracket characters:
 * 1) Create the XML file.
 * 2) Create the Visual Basic .NET application, and then insert the code.

back to the top

Create the XML File
Copy and paste the following code into Notepad, and then save the file as customers.xml :    BLAUS Blauer See Delikatessen Hanna Moos test </Customer> <Customer> SPLIR</CustomerID> Split Rail Beer & Ale</CompanyName> Art raunschweiger</ContactName> <Region>WY</Region> </Customer> </Customers> back to the top

Create Visual Basic .NET Project
<ol> <li>Create a new Visual Basic .NET Windows application.</li> <li>Use a drag-and-drop operation to move a TextBox, two Button controls, and a DataGrid.</li> <li>Set the Multiline property of the TextBox to True.</li> <li> Import the following namespaces: Imports System.Xml Imports System.IO Imports System.Data.SqlClient </li> <li> After the following section Inherits System.Windows.Forms.Form copy and paste the following code sample: Dim filepath As String = &quot;C:\customers.xml&quot; Private Sub ReplaceSpecialChars(ByVal linenumber As Long) Dim strm As StreamReader Dim strline As String Dim strreplace As String

Dim tempfile As String = &quot;C:\temp.xml&quot; Try FileCopy(filepath, tempfile) Catch ex As Exception MessageBox.Show(ex.Message) End Try

Dim strmwriter As New StreamWriter(filepath) strmwriter.AutoFlush = True

strm = New StreamReader(tempfile)

Dim i As Long = 0 While i < linenumber - 1 strline = strm.ReadLine strmwriter.WriteLine(strline) i = i + 1 End While

strline = strm.ReadLine Dim lineposition As Int32 lineposition = InStr(strline, &quot;&&quot;) If lineposition > 0 Then strreplace = &quot;&amp;&quot; Else lineposition = InStr(2, strline, &quot;<&quot;) If lineposition > 0 Then strreplace = &quot;<&quot; End If       End If        strline = Mid(strline, 1, lineposition - 1) + strreplace + Mid(strline, lineposition + 1) strmwriter.WriteLine(strline)

strline = strm.ReadToEnd strmwriter.WriteLine(strline)

strm.Close strm = Nothing

strmwriter.Flush strmwriter.Close strmwriter = Nothing

End Sub

Public Function LoadXMLDoc As XmlDocument Dim xdoc As XmlDocument Dim lnum As Long Dim pos As Long Dim Newxml As String Try xdoc = New XmlDocument xdoc.Load(filepath) Catch ex As XmlException MessageBox.Show(ex.Message) lnum = ex.LineNumber ReplaceSpecialChars(lnum)

xdoc = LoadXMLDoc

End Try Return (xdoc) End Function </li> <li> Copy and paste the following code into the Click event of Button1: Dim xmldoc As New XmlDocument xmldoc = LoadXMLDoc Dim nextnode As XmlNode nextnode = xmldoc.FirstChild.NextSibling TextBox1.Text = nextnode.OuterXml </li> <li> Copy and paste the following code into the Click event of Button2: Dim ds As New DataSet Dim xdoc As New XmlDocument Dim cnNwind As New SqlConnection(&quot;Data source=myservername;user id=myuser;Password=mypassword;Initial catalog=Northwind;&quot;) Dim daCustomers As New SqlDataAdapter(&quot;Select customerid,companyname,contactname, region from customers where region='WY'&quot;, cnNwind) Dim filepath As String Try daCustomers.Fill(ds, &quot;Customers&quot;) DataGrid1.DataSource = ds.Tables(0) ds.WriteXml(&quot;C:\Dataset.xml&quot;) xdoc.Load(&quot;C:\Dataset.xml&quot;) Dim nextnode As XmlNode nextnode = xdoc.FirstChild.NextSibling TextBox1.Text = nextnode.OuterXml.ToString Catch ex As Exception MessageBox.Show(ex.Message) End Try </li> <li>Change the server name, the user name, and the password to connect to your server that is running Microsoft SQL Server.</li> <li>Build the project, and then run it.</li> <li>Click Button1.

The errors that you receive are consistent with the description of the errors that are explained in the &quot;Required: An XML File with Special Characters&quot; section. The XML data is then displayed in the TextBox; the ampersand is replaced with</li> <li>Click Button2.

In the DataGrid, companyname has an ampersand and the TextBox shows the XML data with</li></ol>

back to the top

<div class="references_section">