all groups > dotnet xml > march 2004 >
You're in the

dotnet xml

group:

XMLTextWriter -> utf8 string?


XMLTextWriter -> utf8 string? Bill Cohagan
3/26/2004 7:17:32 PM
dotnet xml:
I'm trying to generate an XML document within a string. I can do this using
the XmlDocument class and it's CreateElement, etc. methods, then at the end
I reference the OuterXML property and I've got it.

What I'd like to do (for various reasons) is to use an XMLTextWriter instead
of the DOM, but I can't figure out how to do this without going to a file.
I've tried creating the XMLTextWriter around a StringWriter and that works,
but produces utf16. I don't see any way of overriding this using the
StringWriter. What am I missing?

BTW, the problem is that the consumer of the XML can't handle utf-16.

Thanks in advance,
Bill

Re: XMLTextWriter -> utf8 string? Christoph Schittko [MVP]
3/27/2004 12:51:56 AM
Bill,
strings are always utf-16 encoded. You can use a StreamWriter over a
MemoryStream if you need a different character encoding. Check the group
archives for some examples.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[quoted text, click to view]

Re: XMLTextWriter -> utf8 string? Bill Cohagan
3/27/2004 6:32:11 PM
Christoph
Thanks for the response. I thought that was the case (strings are
utf-16), so perhaps this is an interop problem. The XML string I'm
generating is being fed to QuickBooks via their QBXML interface (which is
COM interop). The string generated via the XMLTextWriter has utf-16
specified in the xml header; while the string generated by the OuterXML
property of an XMLDocument does not. If they are both in fact utf-16 then
it must (or may) be the utf-16 in the XML header that is causing the
problem.

I wonder if that would inhibit conversion to utf-8 (or ASCII) as the string
is converted for use as a string argument to a COM object?

Bill

"Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote in
message news:%23Zjjfg8EEHA.2416@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

Re: XMLTextWriter -> utf8 string? v-kevy NO[at]SPAM online.microsoft.com
3/29/2004 11:59:34 AM
Hi Bill,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to write Xml to an in memory
stream without creating a file. If there is any misunderstanding, please
feel free to let me know.

As Christoph mentioned, in memory strings are always UTF-16 encoded. It is
stream that can be encoded to UTF-8. I think you can use the following
overload of XmlTextWriter constructor to generate the Xml you need.

XmlTextWriter Constructor (Stream, Encoding);

Use a MemoryStream object as the first parameter and Encoding.UTF8 as the
second. It will create an in-memory stream with UTF8 encoding for you.

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Re: XMLTextWriter -> utf8 string? v-kevy NO[at]SPAM online.microsoft.com
3/31/2004 1:07:29 PM
Hi Bill,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Re: XMLTextWriter -&gt; utf8 string? DotNetJunkies User
5/14/2004 7:40:23 AM
I'm a very much similar issue and would like to know if you could provide me with an answer, this is what I'm trying but it's not working:

//I'll be writing to this in memory stream
MemoryStream stream = new MemoryStream();

//Using this xml writer with UTF8 encoding
XmlTextWriter xmlTxtWrtr = new XmlTextWriter(stream,Encoding.UTF8);
//In the stream I'm placing the result of applying a XSL transform
xslTransform.Transform(xmlDocInterm.CreateNavigator(),null,xmlTxtWrtr,null);

//This is trying to get the string out of the stream
byte[] bytesXmlUt8 = new byte[stream.Length];
stream.Seek(0,SeekOrigin.Begin);
stream.Read(bytesXmlUt8,0,(int)stream.Length);
stream.Close();

//This line gets me a string which is junk character no matter
//which encoding I use
string strXmlTransfrmd=Convert.ToBase64String(bytesXmlUt8);

//This will be the output XML UTF-8 encoded
XmlDocument xmlDocOutput = new XmlDocument();

//Of course, the string is not valid xml, so it won't load
xmlDocOutput.LoadXml(strXmlTransfrmd);


---
Posted using Wimdows.net NntpNews Component -

Re: XMLTextWriter -&gt; utf8 string? Oleg Tkachenko [MVP]
5/16/2004 12:21:51 PM
[quoted text, click to view]

Could you explain, what your code should accomplish?

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button