473,394 Members | 2,048 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,394 software developers and data experts.

XmlWriter / XmlTextWriter corrupt chars...

Mat
Hi all.
I can't understand the problem with my code...
I wrote a simple test function to write an XML document and in the
resulting file I find 3 strange chars before the document starting.

=== CODE ===

using( XmlTextWriter w = new
XmlTextWriter("test2.xml",System.Text.Encoding.UTF 8) )
{
w.Namespaces = false;
w.Indentation = 4;
w.Formatting = Formatting.Indented;
w.WriteStartDocument();
w.WriteStartElement("root");
w.WriteStartElement("item");
w.WriteAttributeString("attr","123");
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Close();
}

===

And the result I get is:

===

<?xml version="1.0" encoding="utf-8"?>
<root>
<item attr="123" />
</root>

===

Help me please!

Oct 24 '06 #1
3 2886
Mat

Mat wrote:
>
And the result I get is:

===

<?xml version="1.0" encoding="utf-8"?>
<root>
<item attr="123" />
</root>

===

Help me please!
Sorry, these chars aren't ASCII standards so they are not visible
here...
Value in HEX:

EF BB BF

Oct 24 '06 #2
These are Unicode Byte Order Mark and they are ok.
But if you really really want to omit them (BOM is optional in UTF-8),
use this:
XmlTextWriter("test2.xml", new System.Text.UTF8Encoding(false)) )

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Mat wrote:
Hi all.
I can't understand the problem with my code...
I wrote a simple test function to write an XML document and in the
resulting file I find 3 strange chars before the document starting.

=== CODE ===

using( XmlTextWriter w = new
XmlTextWriter("test2.xml",System.Text.Encoding.UTF 8) )
{
w.Namespaces = false;
w.Indentation = 4;
w.Formatting = Formatting.Indented;
w.WriteStartDocument();
w.WriteStartElement("root");
w.WriteStartElement("item");
w.WriteAttributeString("attr","123");
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Close();
}

===

And the result I get is:

===

<?xml version="1.0" encoding="utf-8"?>
<root>
<item attr="123" />
</root>

===

Help me please!
Oct 24 '06 #3
Mat

Oleg Tkachenko [MVP] wrote:
These are Unicode Byte Order Mark and they are ok.
But if you really really want to omit them (BOM is optional in UTF-8),
use this:
XmlTextWriter("test2.xml", new System.Text.UTF8Encoding(false)) )
Perfect !
Thank you very much Oleg.

Just for reference:
http://en.wikipedia.org/wiki/Byte_Order_Mark
I learnt a new thing :)

Oct 24 '06 #4

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

Similar topics

1
by: Linda Chen | last post by:
I need to write some unicode symbols such as degree symbol (for example 36°) by using XMLTextWrite but couldn't make it work. I found the degree char in unicode is '\u030A' and here is my sample...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
4
by: Einar Høst | last post by:
Hi, I'm having weird problems using StringWriter and XmlTextWriter. My code looks like this: StringWriter sw = new StringWriter(CultureInfo.InvariantInfo); XmlTextWriter xtw = new...
1
by: jschell | last post by:
The following two cases behave differently in Net 2.0. The Case_Create_StringWriter throws an exception while the Case_XmlTextWriter does not. Is there a problem with this code? Or is this a...
4
by: Bob | last post by:
Hi Need to produce a Doc with no encoding info. Is there anyway of doing this? Thanks Bob i.e. <?xml version=\"1.0\" ?>
2
by: Harry | last post by:
Hi, I am using XMLWriter to build xml and I need to build the element below. How do I do this? <Password format="encrypted">password</Password> Thanks
2
by: Simon Harvey | last post by:
Hi all, I'm having a real problem with getting an XMLWriter as a result of an xsl tranform I'm attempting. My code is: private void btnPerformTransform_Click(object sender, EventArgs e) {...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
1
by: JAM | last post by:
I'm trying to code some directory structure as my output / input file using XML. I would like to see formatiing with indentations. mimicking directory tree structure. Unfortunately the code such as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.