473,327 Members | 2,055 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,327 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 11454
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.