Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2006

Tip: Looking for answers? Try searching our database.

XmlWriter / XmlTextWriter corrupt chars...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mat - 24 Oct 2006 11:44 GMT
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.UTF8) )
{
   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!
Mat - 24 Oct 2006 11:47 GMT
> And the result I get is:
>
[quoted text clipped - 8 lines]
>
> Help me please!

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

EF BB BF
Oleg Tkachenko [MVP] - 24 Oct 2006 13:05 GMT
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)) )

Signature

Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net

> Hi all.
> I can't understand the problem with my code...
[quoted text clipped - 33 lines]
>
> Help me please!
Mat - 24 Oct 2006 13:24 GMT
> 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 :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.