473,390 Members | 1,280 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,390 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 11463
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: 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:
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?
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
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...
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.