Issue with VB.Net, XML and encoding

exiletgm

Member
Joined
May 14, 2007
Messages
14
Programming Experience
1-3
I'm attempting to teach myself XML integration into VB.Net
I've created some code that creates a XML document but I am having and issue with the encoding information being entered into the XML document.

VB.NET:
Imports System
Imports System.Xml
Public Class Form1

    Private Sub btnCreateDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateDocument.Click
        ' Create the XmlDocument.
        Dim docXML As XmlDocument = New XmlDocument()
        docXML.LoadXml( _
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
        "<?xml-stylesheet href=""style.css"" type=""text/css""?>" & _
        "<SigBlocks>" & _
        "<SigBlock BlockName=""Dan - Work"">" & _
        "<FName>Dan</FName>" & _
        "<FNameChecked>False</FNameChecked>" & _
        "<LName>Mabbutt</LName>" & _
        "<LNameChecked>False</LNameChecked>" & _
        "<Address>About Visual Basic</Address>" & _
        "<AddressChecked>True</AddressChecked>" & _
        "<Phone>(555) 555-5555</Phone>" & _
        "<PhoneChecked>False</PhoneChecked>" & _
        "<SSN>123-45-6789</SSN>" & _
        "<SSNChecked>False</SSNChecked>" & _
        "<Email>visualbasic@about.com</Email>" & _
        "<EmailChecked>True</EmailChecked>" & _
        "</SigBlock>" & _
        "<SigBlock BlockName=""Dan - Personal"">" & _
        "<FName>Dan</FName>" & _
        "<FNameChecked>True</FNameChecked>" & _
        "<LName>Mabbutt</LName>" & _
        "<LNameChecked>True</LNameChecked>" & _
        "<Address>Home Sweet Home</Address>" & _
        "<AddressChecked>False</AddressChecked>" & _
        "<Phone>(123) 456-7890</Phone>" & _
        "<PhoneChecked>True</PhoneChecked>" & _
        "<SSN>123-45-6789</SSN>" & _
        "<SSNChecked>False</SSNChecked>" & _
        "<Email>me@MyOwnDomain.com</Email>" & _
        "<EmailChecked>False</EmailChecked>" & _
        "</SigBlock>" & _
        "</SigBlocks>")

        ' Save the document to a file and auto-indent the output.
        Dim writer As XmlTextWriter = New XmlTextWriter("e:\Name.xml", Nothing)
        writer.Formatting = Formatting.Indented
        docXML.Save(writer)
    End Sub
End Class

When the document is created the first line looks like
VB.NET:
<?xml version="1.0"?>
instead of
VB.NET:
<?xml version="1.0" encoding="UTF-8"?>
like I'm expecting.
I'm not even sure if it is even very important but I'm curious why it is doing this.

Thanks a bunch,
Grant
 
I guess it is becuase of the XmlTextWriter Constructor usage:
XmlTextWriter Constructor (String, Encoding) :
encoding
The encoding to generate. If encoding is a null reference (Nothing in Visual Basic) it writes out the stream as UTF-8 and omits the encoding attribute from the ProcessingInstruction.
And string concenating a Xml document? You would normally save the Xml string as a plain text file with .xml extension and use the Load method.
 
You have to put something in the encoding parameter other than Nothing and the default System.Text.Encoding.UTF8. Well, perhaps if you did put utf8 there explicitly it would spell it out, try if you like, but it shouldn't be any different to handle since utf8 is default when not specified.
 
Worked like a charm.

VB.NET:
Dim writer As XmlTextWriter = New XmlTextWriter("e:\Name.xml", System.Text.Encoding.UTF8)

Thanks so much :)
 
Back
Top