473,386 Members | 1,720 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,386 software developers and data experts.

XmlTextWriter and Text.Encoding.UTF8 issues

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 having issues using XmlTextWriter, saving it out to a file with UTF8
encoding, and seeing "dirty", or "human unreadable" characters show up
*right before* the XML declaration.
I need to have the XML declaration state "encoding = utf-8", but also get
rid of the dirty characters. I was hoping that using
System.Text.Encoding.UTF8 in the constructor of the XmlTextWriter class
would prove nice and simple, but I'm finding out I'm having some issues with
it.

Ultimately, I want to stream the XMLDocument over to the vendor via HTTP
POST, but right now, the vendor's saying my transmissions have been invalid,
perhaps due to the dirty chars prior to the XML declaration. I included
code for both my local file dumping activities and my streaming activities.
Code snippets:
----------------------------------------------------------------------------
--------------------------------------------------------

XmlDocument xDoc = <some XmlDocument>;
/*

For following line of code, no Explicit Encoding Here, but encoding is
utf-16, according to XML declaration. Vendor wants it to say "utf-8".--
Resulting file doesn't have dirty chars prior to XML declaration, as
follows:

"<?xml version="1.0" encoding="utf-16"?>
<wPOREQ payloadID=""............."

*/

xDoc.Save("c:\\temp\\tpg\\" + dtNow + "_XMLPOREQ_Raw.xml");

----------------------------------------------------------------------------
----------------------------------------------------------------------------
--------------------------------------

/* For following line of code, tried dumping XmlDoc to file as utf8, but
notice the dirty chars right before the XML declaration:

<?xml version="1.0" encoding="utf-8"?><wPOREQ payloadID="" ..............

*/

xDoc.Save(new XmlTextWriter(@"c:\temp\tpg\" + dtNow +
"_POREQ_UTF8_ENCODED.xml",System.Text.Encoding.UTF 8));

----------------------------------------------------------------------------
----------------------------------------------------------------------------
--------------------------------------

/* Ultimately, I want to send the XmlDocument over to vendor, using the
following code:

*/

string url = PORequestURL;

System.Net.WebRequest w = System.Net.WebRequest.Create(url);

w.Method = "POST";

w.Timeout = Convert.ToInt32(PORequestTimeout);

w.ContentType = PORequestContentType;

System.IO.Stream s = w.GetRequestStream();

XmlTextWriter writer = new XmlTextWriter(s, System.Text.Encoding.UTF8);

xDoc.Save(writer);

writer.Close();

----------------------------------------------------------------------------
----------------------------------------------------------------------------
--------------------------------------

When I attempted to capture the Response, after sending a Request using the
above code, and dump the Response to a local file using the following code,
I see the same dirty chars show up in the local file:

----------------------------------------------------------------------------
----------------------------------------------------------------------------
--------------------------------------

/*

Dumped file begins with the following:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE cXML.............

*/

System.Net.WebResponse response = w.GetResponse();

XmlDocument xDoc1 = new XmlDocument();

XmlTextReader reader = new XmlTextReader(response.GetResponseStream());

xDoc1.Load(reader);

reader.Close();

xDoc1.Save(new XmlTextWriter(@"c:\temp\tpg\" + dtNow +
"_RESPONSE.xml",System.Text.Encoding.UTF8));

----------------------------------------------------------------------------
----------------------------------------------------------------------------
--------------------------------------

Basically, I need to encode my XML request in utf-8, without the junk
characters before the XML declaration. Any help or pointers would be
gratefully appreciated.

Thanks in advance,

- Howard
Nov 12 '05 #1
4 11462
The standard UTF8 encoding that you get from Encoding.UTF8 has the utf8
identifier on by default. That's the "invalid" characters you see
before the xml declaration.

For clients that don't understand this identifier, you have to use the
constructor on the UTF8Encoding class where you turn off the
identifier.

Encoding enc = new UTF8Encoding(false);

Nov 12 '05 #2
The standard UTF8 encoding that you get from Encoding.UTF8 has the utf8
identifier on by default. That's the "invalid" characters you see
before the xml declaration.

For clients that don't understand this identifier, you have to use the
constructor on the UTF8Encoding class where you turn off the
identifier.

Encoding enc = new UTF8Encoding(false);

Nov 12 '05 #3
That did the trick. What a relief!

Any pro's and con's with having a client understand the identifier?

Thank you so much for the help!

- Howard

<cb*****@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
The standard UTF8 encoding that you get from Encoding.UTF8 has the utf8
identifier on by default. That's the "invalid" characters you see
before the xml declaration.

For clients that don't understand this identifier, you have to use the
constructor on the UTF8Encoding class where you turn off the
identifier.

Encoding enc = new UTF8Encoding(false);

Nov 12 '05 #4
H Lee wrote:
That did the trick. What a relief!

Any pro's and con's with having a client understand the identifier?


In fact byte-order mark (BOM) is optional in UTF-8 so don't worry about it.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #5

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

Similar topics

4
by: Chris | last post by:
Hi, I'm stumped and could use some suggestions. I'm trying to serialize some data to an XML fragment in string form, then deserialize it from a string back to an XML fragment, from which I...
1
by: Johan Klijn | last post by:
Can somebody explain why the endElement (</items>) in the following sample is not written? Public Sub Test() Dim objMem As New MemoryStream Dim objWriter As New XmlTextWriter(objMem, New...
5
by: Adam Rothberg | last post by:
The following code sample should produce a valid xml file to the console. However, when I try this in C# (Visual Studio 2003, 1.1 Framework), there is an extra questionmark preceding the rest of...
3
by: K Rege | last post by:
Hi there, I tried to implement a little XML-EchoServer using Sockets and XmlTextWriter/Reader. However the server hangs while reading the input from the client (wr.Flush() seams not to work). I...
2
by: Ghost | last post by:
Can anybody how do I use XmlTextWriter example? I want to write such XML file: <?xml version="1.0" standalone="yes"?> <Clients> <Client> <Name>David</Name> <Age>27</Age> </Client>
5
by: Robert Dickow | last post by:
Hi, My XML files created with XmlTextWriter routines has been resulting in working files, but they include three bytes of garbage at the start of the file. The bytes are: $EF $BB $BF (I think...
1
by: Mori | last post by:
I want to use the XMLTextWriter to build and xml stream and then display or pass the xml to another method. I do not wish to persist the xml. I am using the Memory stream to store the xml but I...
2
by: Mori | last post by:
I am using the following code to manufactue xml using xmltextwriter. The xml is okay except for the extra set of start document and the outer <string> element. Where do they come from and how do...
1
by: hharry | last post by:
hi all i have the following class: <Serializable()> _ Public Class MVR_Inputs Public FirstName As String Public MiddleName As String Public LastName As String Public Suffix As String
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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
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...

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.