I create a dataset with a table. The table has two columns(for keys and values), and I add a row to the table and are trying to use different overloaded versions of dataset.writeXml functions to save the file with the correct encoding (I want utf-8), but when I save with encoding utf-8 the header in the file is missing + more:
Example 1:
Dim myFileStream As New System.IO.FileStream(ConfigFile, System.IO.FileMode.Create)
Dim myXmlWriter As New System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.UTF8)
DSoptions.WriteXml(myXmlWriter)
myXmlWriter.Close()
Output (linebreaks missing, heading missing, file is not saved as utf-8 but ansi):
<Configurator><ConfigOptions><OptionName>var11</OptionName><OptionValue>val1</OptionValue></ConfigOptions></Configurator>
--------------------
Example 2:
Dim myFileStream As New System.IO.FileStream(ConfigFile, System.IO.FileMode.Create)
Dim s As New System.IO.StreamWriter(myFileStream, System.Text.Encoding.UTF8)
DSoptions.WriteXml(s)
s.Close()
Output (the heading is missing, but linebreaks are included and the file is saved with utf-8).
<Configurator>
<ConfigOptions>
<OptionName>var11</OptionName>
<OptionValue>val1</OptionValue>
</ConfigOptions>
</Configurator>
---------------
Example 3:
same as above but with schema:
DSoptions.WriteXml(myFileStream, XmlWriteMode.WriteSchema)
<Configurator>
<xs:schema id="Configurator" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Configurator" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ConfigOptions">
<xs:complexType>
<xs:sequence>
<xs:element name="OptionName" type="xs:string" minOccurs="0" />
<xs:element name="OptionValue" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<ConfigOptions>
<OptionName>var11</OptionName>
<OptionValue>val1</OptionValue>
</ConfigOptions>
</Configurator>
-------------------
Example 4:
DSoptions.WriteXml("C:\Temp\test.xml")
Output (Is not saved as utf-8)
< xml version="1.0" standalone="yes" >
<Configurator>
<ConfigOptions>
<OptionName>var11</OptionName>
<OptionValue>val1</OptionValue>
</ConfigOptions>
</Configurator>
I would like to fix Example 2 so I also can include the heading (or fix Example 1 so it has linebreaks and heading). Is this possible

write xml and encoding
xiaomaolover
I's strange. I tried excample 1 and it worked for me as expected.
I rewrited it a bit the way it is recomented in V2:
FileStream myFileStream = new FileStream(@"c:\test.xml", System.IO.FileMode.Create); XmlWriterSettings ws = new XmlWriterSettings();ws.Encoding = System.Text.
Encoding.UTF8;ws.Indent =
true; XmlWriter myXmlWriter = XmlWriter.Create(myFileStream, ws);myXmlWriter.WriteStartElement(
"root");myXmlWriter.WriteElementString(
"foo", "bar");myXmlWriter.WriteEndElement();
myXmlWriter.Close();
Nick77
Sorry but I entered a squere character in my last post to show you what was saved in example 1, and suddenly the text in the entire thread was messed up...
fan2005
I am using vb.net 2.0
If I use example 1, and then open the file in notepad, - Ansi is displayed in the file save as dialog box Encoding-field.
If I use example 2, and open the file in notepad, UTF-8 is displayed in the file save as dialog box Encoding-field.
(I select the save as dialog box just to see what format the file already has been saved in).
Also, I have tried saving some chinese characters with example 1 and example 2,
And the first one just save the text as □, while the last one saves the text as it should.
I am making an application where not only the GUI is multi lingual, but I must also expect the user to type in data in 'any' language (with some limitations), and so I can not use codepage 1252 which I used previously.
As for now, I am using example 2 to be able to save as utf-8 because I have got the impression this is the best format to use.
If you have any ideas why I cant get example 1 to work properly I would be very happy.
BennyAW
Example 1 gives you more control over the XML you write.
I am surprised that it doesn't save in UTF-8. It should. Can you double check
What version of Framework are you on
XmlTestWriter has properties which control the serialization. In particular to output linebreakes you should set Formatting and property. (you may also like to play with Indentation and IndentChar properties).
In .NET Framework 2.0 recommended way to instantiate XmlTestWriter is by use of XmlWriter.Create() method. You can pass all required properties with 'settings' parameter of XmlWriterSettings type.
Output of headings has nothing to do with XmlWriter and is controlled by DataSet itself. You can ask abut this in the .NET Framework Data Access and Storage forum.