Simon Haynes

I'm not sure whether I'm doing something wrong, but I'm using the XMLWriter class to save a few items to an file and when I view the saved file in Wordpad afterwards I can see random chinese pictogram characters, some of them in the actual tags themselves.

I'm using the class like this:

Using Writer As XmlWriter = XmlWriter.Create(XMLFileWithPath, XMLFile)

Try

Writer.WriteStartElement("Scenes")

For Inner = 1 To Scenes.Count

Writer.WriteStartElement("Scene")

TempScene = cProject.SceneByIndex(Inner)

Writer.WriteElementString("ID", TempScene.ID)
Writer.WriteElementString("Title", TempScene.Title)
Writer.WriteElementString("Desc", TempScene.DescText)

Writer.Flush()

(etc, etc)

Is it necessary to use Flush after every 'WriteElementString' And should I be doing something completely different I modelled my code on an example found on the web somewhere.

Here's an example from the output file... not sure whether it'll display correctly:

<Status>1</StaôÎus>




Re: Visual Basic Language XMLWriter bug or pebcak?

Bruno Yu - MSFT

Simon,

1. Please make sure that the Encoding of your Browser is in Unicode (UTF-8).

2. Please take a look at the WriteElementString method and make sure that the usage of the Flush. When overridden in a derived class, flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.

http://msdn2.microsoft.com/en-us/library/aa336074(VS.71).aspx

3. I believe the following thread can help you to understand your problem on using XML in VB 2005:

Code Snippet

'<Insert VB Code HERE...>

With xWriter

.WriteComment("I created a header element to put some information about ")

.WriteComment(" the file I am creating, such as timestamps, record counts ")

.WriteComment(" etc, etc")

'I do this because if I create too large of a file, I dont have to parse the

'entire file to read pertenant information and the info is readily available

'right at the top of the file and can be viewed quickly with an XML stream

.WriteStartElement("header")

.WriteElementString("recordcount", iTotalRecords)

.WriteElementString("recorddate", Format(IO.File.GetLastWriteTime(sPath), "yyyy-MM-dd"))

.WriteElementString("user", (System.Environment.UserDomainName & "\" & _

System.Environment.UserName))

.WriteElementString("importdate", Now.ToShortDateString.ToString)

'Here I close the header element

.WriteEndElement

End With

'<Other VB Code Here....>

For iCurrentRecord = 1 to iTotalRecords

With xWriter

If sVariable1 <> Nothing Then

.WriteElementString("sVariable1Name", sVariable1)

End If

If sVariable2 <> Nothing Then

.WriteElementString("sVariable2Name", sVariable2)

End If

If sVariable3 <> Nothing Then

.WriteElementString("sVariable3Name", sVariable3)

End If

End With

Next

With xWriter

'Close Root Element

.WriteEndElement()

.WriteEndDocument()

.Flush()

.Close()

End With