473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

XML Encoding woes...

I use the following code to create an XML string:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tw As New StringWriter
Dim xml As New XmlTextWriter(tw)
xml.WriteStartDocument()
xml.WriteStartElement("StartElement")
xml.WriteElementString("ElementString", "TheElement")
xml.WriteEndElement()
xml.WriteEndDocument()
xml.Close()
MsgBox(tw.ToString)
End Sub

This produces

<?xml version="1.0" encoding="utf-16"?>
<StartElement>
<ElementString>TheElement</ElementString>
</StartElement>

I need the encoding to say "utf-8", but I can't get it to work.

among other things, I've tried adding

xml.Settings.Encoding = New UTF8Encoding(False)

directly after Dim'ing the xml object, but it throws the "Object reference
not set to an instance" error.

Need some guidance on this one.
Jun 3 '06 #1
4 1858
Hello Terry,

Look at the constructor overloads for XmlTextWriter.

-Boo
I use the following code to create an XML string:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tw As New StringWriter
Dim xml As New XmlTextWriter(tw)
xml.WriteStartDocument()
xml.WriteStartElement("StartElement")
xml.WriteElementString("ElementString", "TheElement")
xml.WriteEndElement()
xml.WriteEndDocument()
xml.Close()
MsgBox(tw.ToString)
End Sub
This produces

<?xml version="1.0" encoding="utf-16"?>
<StartElement>
<ElementString>TheElement</ElementString>
</StartElement>
I need the encoding to say "utf-8", but I can't get it to work.

among other things, I've tried adding

xml.Settings.Encoding = New UTF8Encoding(False)

directly after Dim'ing the xml object, but it throws the "Object
reference not set to an instance" error.

Need some guidance on this one.

Jun 4 '06 #2
Yes, but they require that I write to a Stream, and not a StringWriter,
which makes things a little more difficult. How can I use a Stream to write
a string? I don't want to have to declare a large byte array.

"GhostInAK" <gh*******@gmail.com> wrote in message
news:c7*************************@news.microsoft.co m...
Hello Terry,

Look at the constructor overloads for XmlTextWriter.

-Boo
I use the following code to create an XML string:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tw As New StringWriter
Dim xml As New XmlTextWriter(tw)
xml.WriteStartDocument()
xml.WriteStartElement("StartElement")
xml.WriteElementString("ElementString", "TheElement")
xml.WriteEndElement()
xml.WriteEndDocument()
xml.Close()
MsgBox(tw.ToString)
End Sub
This produces

<?xml version="1.0" encoding="utf-16"?>
<StartElement>
<ElementString>TheElement</ElementString>
</StartElement>
I need the encoding to say "utf-8", but I can't get it to work.

among other things, I've tried adding

xml.Settings.Encoding = New UTF8Encoding(False)

directly after Dim'ing the xml object, but it throws the "Object
reference not set to an instance" error.

Need some guidance on this one.


Jun 4 '06 #3
Hello Terry,
As you no doubt noticed the XMLStringWriter ctor has an overload that takes
a TextWriter. This is the only overload that does not take an Encoding parameter
(besidesthe default ctor) because it is assumed that the TextWriter will
implement the correct Encoding. TextWriter is declared MustInherit (Abstract
in C#).

So... Create a new class (I suggest naming it UTF8TextWriter). Inherit from
System.IO.TextWriter. You will have to implement the Encoding proeprty (which
is declared MustOverride in the base). Return the correct Encoding (as a
member variable (field)).

Then pass an instance of this new class to the XMLStringWriter's ctor. Presto,
voila, abracadabra! :)

Enjoy,
-Boo
Yes, but they require that I write to a Stream, and not a
StringWriter, which makes things a little more difficult. How can I
use a Stream to write a string? I don't want to have to declare a
large byte array.

Jun 6 '06 #4
I had this problem to ,,, so after some strugling i solved it like this

i created a encodedstringwriter

here is my full implementation , use the parts you like / strip what you
don`t need

Imports System.Text
Imports System.IO

Namespace Text

'''<summary>

''' Implementeerd een stringwriter waarbij je zelf de encoding kunt kiezen

''' de data wordt opgeslagen in een stringbuilder

''' </summary>

Public Class EncodedStringWriter

Inherits StringWriter

'Private property setter

Private _Encoding As Encoding

Public Sub New(ByVal sb As StringBuilder, ByVal Encoding As Encoding)

MyBase.New(sb)

_Encoding = Encoding

End Sub

''' <summary>

'''

''' </summary>

''' <value></value>

''' <doc>

''' <summary>getter voor de string encoding </summary>

''' <returns>de Encoding waarin wordt geschreven </returns>

''' <filterpriority>1</filterpriority>

''' </doc>

Public Overrides ReadOnly Property Encoding() As Encoding

Get

Return _Encoding

End Get

End Property

End Class

End Namespace
Now in my XML base Class

Option Explicit On
Option Strict On

Imports System.Xml

Imports System.Text

Imports System.IO

Public MustInherit Class ClsXMLBase

Public Structure XMLElement

Dim ElementName As String

Dim ElementValue As String

End Structure

Public Sub New()

'-- default

Init()

End Sub

Public Sub New(ByVal XMLRootName As String)

RootNodeName = XMLRootName

Init()

End Sub

Private Mvar_XMLDoc As XmlTextWriter

Private Mvar_sb As StringBuilder

Public Property ObjXML() As XmlTextWriter

Get

If IsNothing(Mvar_XMLDoc) Then

Init()

End If

Return Mvar_XMLDoc

End Get

Set(ByVal value As XmlTextWriter)

Mvar_XMLDoc = value

End Set

End Property

Private Mvar_RootNodeName As String = "MyRootnode"

''' <summary>

''' Getter Setter voor de rootnode naam

''' </summary>

''' <value>De naam van de rootnode .</value>

Public Property RootNodeName() As String

Get

Return Mvar_RootNodeName

End Get

Set(ByVal value As String)

Mvar_RootNodeName = value

End Set

End Property

Public Overridable Sub Reset()

Init()

End Sub

Private Sub Init()

Mvar_sb = New StringBuilder(500)

Mvar_XMLDoc = New XmlTextWriter(New Text.EncodedStringWriter(Mvar_sb,
Encoding.UTF8))

Mvar_XMLDoc.Formatting = Formatting.Indented

Mvar_XMLDoc.Indentation = 2

Mvar_XMLDoc.WriteStartDocument()

Mvar_XMLDoc.WriteStartElement(RootNodeName)

EndWritten = False

End Sub

''' <summary>

''' sluit de instance van het XML object en alle onderliggende streams

''' </summary>

Public Sub Close()

Mvar_XMLDoc.Close()

End Sub

''' <summary>

''' geeft de XML terug als een string

''' </summary>

''' <returns>string data </returns>

Public Function XMLToString() As String

If Not EndWritten Then WriteEndDocument()

Return Mvar_sb.ToString

End Function

Private EndWritten As Boolean

Private Sub WriteEndDocument()

Mvar_XMLDoc.WriteEndElement()

Mvar_XMLDoc.WriteEndDocument()

EndWritten = True

End Sub

End Class

and finally your worker class

ption Explicit On

Option Strict On

Public Class clsYourWorkerclass

Inherits ClsXMLBase

#Region " constructors "

''' <summary>

''' Initializeeerd een nieuwe instance van de class.

''' Overloaded

''' </summary>

''' <param name="RootNodeName">Naam van de rootnode ( standaard is NeProS )
..</param>

Public Sub New(ByVal RootNodeName As String)

MyBase.New(RootNodeName)

End Sub

#End Region

''' <summary>

''' Schrijft het XML document naar een file

''' </summary>

''' <param name="TargetPath">het doel path (VB: C:\test.xml ) </param>

Public Sub WriteXMLDocAsFile(ByVal TargetPath As String)

My.Computer.FileSystem.WriteAllText(TargetPath, MyBase.XMLToString, False)

End Sub

End class
use the mybase prefix to see all methods of the xml doc

you have now a reusable xml document wich you can use with anny encoding you
like
regards

Michel Posseth [MCP]
"Terry Olsen" <to******@hotmail.com> schreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
Yes, but they require that I write to a Stream, and not a StringWriter,
which makes things a little more difficult. How can I use a Stream to
write a string? I don't want to have to declare a large byte array.

"GhostInAK" <gh*******@gmail.com> wrote in message
news:c7*************************@news.microsoft.co m...
Hello Terry,

Look at the constructor overloads for XmlTextWriter.

-Boo
I use the following code to create an XML string:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tw As New StringWriter
Dim xml As New XmlTextWriter(tw)
xml.WriteStartDocument()
xml.WriteStartElement("StartElement")
xml.WriteElementString("ElementString", "TheElement")
xml.WriteEndElement()
xml.WriteEndDocument()
xml.Close()
MsgBox(tw.ToString)
End Sub
This produces

<?xml version="1.0" encoding="utf-16"?>
<StartElement>
<ElementString>TheElement</ElementString>
</StartElement>
I need the encoding to say "utf-8", but I can't get it to work.

among other things, I've tried adding

xml.Settings.Encoding = New UTF8Encoding(False)

directly after Dim'ing the xml object, but it throws the "Object
reference not set to an instance" error.

Need some guidance on this one.



Jun 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mark | last post by:
O, woe is me, to have seen what I have seen, see what I see! (That's Shakespeare for those who were wondering what I'm on about) I am "having fun" with cookies. And I wonder if I have...
0
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade...
3
by: Angel Cat | last post by:
Trying to get my jobs to send mail when job fails. Should be easy but it's giving me headache Had a whole slew of issues. Outlook is installed with a n outlook mail profile set up that can...
2
by: Andrew Thompson | last post by:
- NN 4.78 rendering woes, links at far left - I am trying to rework an old site, make it valid html and css, improving the x-browser and 'older browser' compatibility. My efforts so far, have...
0
by: Arun Bhalla | last post by:
I'm having some inconsistency problems with my deployment project ("Setup") and its custom actions ("Installer"). I'm using Visual Studio .NET 2003 (.NET 1.1, no service pack) on Windows XPSP1. ...
3
by: Diez B. Roggisch | last post by:
Hi, I've got to deal with a pretty huge XML-document, and to do so I use the cElementTree.iterparse functionality. Working great. Only trouble: The guys creating that chunk of XML - well, lets...
9
by: Mark Rae | last post by:
Hi, This time, I'm looking for a regular expression which says "the string must contain exactly seven or exactly eight digits" e.g. 123456 fails 1234567 passes 12345678 passes 123456789...
1
by: hdogg | last post by:
Scope Woes - IF statement nested in WHILE statement -PHP I have an array $actuals_sum. <?php while(conditions) { if($i == '24) {
4
by: =?Utf-8?B?VkIgSm9ubmll?= | last post by:
I am at my witless end here, please help! I have an ASP.Net aspx web page, hosted on Windows Server 2003, that receives a query string with the path to an autocad drawing file selected from a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.