473,766 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to obtain a utf-8 string from an XmlReader?

Hi all,

I'm trying to convert the xml obtained from a XmlReader object into a UTF-8
array. My general idea is to read the XmlReader and write into a
MemoryStream. Then convert the MemoryStream bytes into utf-8.

MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(m s, new UTF8Encoding(fa lse));

writer.Formatti ng = Formatting.Inde nted;
writer.Namespac es = false;
writer.Indentat ion = 4;

while(xmlReader .Read())
{
xmlWriter.Write (?);
}

xmlWriter.Flush ();
xmlWriter.Close ();

string xml_as_utf8 = Encoding.UTF8.G etString(ms.ToA rray());

But I fill the XmlReader and XmlWriter are not made for this purpose.
xmlReader.Read( ) parses the xml stream, and xmlWriter is done to create xml
element by element.
Which is the correct strategy here?

Thanks in advance
Sammy

Oct 20 '08 #1
2 5590
SammyBar wrote:
Hi all,

I'm trying to convert the xml obtained from a XmlReader object into a UTF-8
array. My general idea is to read the XmlReader and write into a
MemoryStream. Then convert the MemoryStream bytes into utf-8.

MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(m s, new UTF8Encoding(fa lse));

writer.Formatti ng = Formatting.Inde nted;
writer.Namespac es = false;
writer.Indentat ion = 4;

while(xmlReader .Read())
{
xmlWriter.Write (?);
}

xmlWriter.Flush ();
xmlWriter.Close ();

string xml_as_utf8 = Encoding.UTF8.G etString(ms.ToA rray());

But I fill the XmlReader and XmlWriter are not made for this purpose.
xmlReader.Read( ) parses the xml stream, and xmlWriter is done to create xml
element by element.
Which is the correct strategy here?
What exactly is it that you want to achieve?
Strings in the .NET framework are always UTF-16 encoded.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 20 '08 #2
I found the following solution: deserialize the text reader into a
XmlDocument object, and then deserialize the it into the MemoryStream.
"SammyBar" <sa******@gmail .comescribió en el mensaje
news:Og******** ******@TK2MSFTN GP02.phx.gbl...
Hi all,

I'm trying to convert the xml obtained from a XmlReader object into a
UTF-8 array. My general idea is to read the XmlReader and write into a
MemoryStream. Then convert the MemoryStream bytes into utf-8.

MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(m s, new UTF8Encoding(fa lse));

writer.Formatti ng = Formatting.Inde nted;
writer.Namespac es = false;
writer.Indentat ion = 4;

while(xmlReader .Read())
{
xmlWriter.Write (?);
}

xmlWriter.Flush ();
xmlWriter.Close ();

string xml_as_utf8 = Encoding.UTF8.G etString(ms.ToA rray());
This is an error: The memory stream is already utf-8 encoded. Then this
previous line re-encodes from utf-8 to the .NET standard encoding so we loss
the encoding when converting.
The solution I found here looks like a hack: to deceive the .NET by reading
the utf-8 saved stream as default encoding:
string xml_data = Encoding.Defaul t.GetString(ms. ToArray());

The code fragments looks like this: (note my input is SqlXml sqlXml
variable)
// load

XmlReader xmlReader = sqlXml.CreateRe ader();

XmlDocument xDoc = new XmlDocument();

xDoc.Load(xmlRe ader);

xmlReader.Close ();
// save

MemoryStream ms = new MemoryStream();

XmlTextWriter writer = new XmlTextWriter(m s, new UTF8Encoding(fa lse));

writer.Formatti ng = Formatting.Inde nted;

writer.Indentat ion = 4;

xDoc.Save(write r);

writer.Close();

ms.Close();

// convert to utf-8

//string xml_data = Encoding.UTF8.G etString(ms.ToA rray());

string xml_data = Encoding.Defaul t.GetString(ms. ToArray());


Oct 20 '08 #3

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

Similar topics

1
2777
by: Dominik Jain | last post by:
Hi! We hope somebody can help me with the following: We have a form through which unicode data might be submitted. We need to be able to detect when this happens and, most importantly, we need to be able to obtain the unicode character codes for each individual letter/symbol. Is this possible in PHP? If so, can you please provide a short code snippet. We looked at the mbstring extension but found nothing helpful, especially
3
3384
by: aa | last post by:
Is it OK to include an ANSI file into a UTF-8 file?
38
5739
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I find an answer to this question (don't find it in the W3C_char_entities document). -- Haines Brown brownh@hartford-hwp.com
1
15602
by: stevelooking41 | last post by:
Can someone explain why I don't seem unable to use document.write to produce a valid UTF-8 none breaking space sequence (Hex: C2A0) ? I've tried everyway I've been able to find to tell the browser I'm trying to print UTF-8 and still no luck. I'd like the first 2 tries to match the second two tries as far as output. <HTML> <meta http-equiv="Content-Type" content="application/x-script; charset=UTF-8">
1
2021
by: David Bertoni | last post by:
Hi all, I'm trying to resolve what appears to me an inconsistency in the XML 1.0 recommendation involving entities encoding in UTF-16 and the requirement for a byte order mark. Section 4.3.3 has the following text: http://www.w3.org/TR/REC-xml/#charencoding
6
13892
by: archana | last post by:
Hi all, can someone tell me difference between unicode and utf 8 or utf 18 and which one is supporting more character set. whic i should use to support character ucs-2. I want to use ucs-2 character in streamreader and streamwriter. How unicode and utf chacters are stored.
7
12151
by: Jimmy Shaw | last post by:
Hi everybody, Is there any SIMPLE way to convert from UTF-16 to UTF-32? I may be mixed up, but is it possible that all UTF-16 "code points" that are 16 bits long appear just the same in UTF-32, but with zero padding and hence no real conversion is necessary? If I am completely wrong and some intricate conversion operation needs to take place, can anyone give me some primer on the subject?
10
19579
by: Jed | last post by:
I have a form that needs to handle international characters withing the UTF-8 character set. I have tried all the recommended strategies for getting utf-8 characters from form input to email message and I cannot get it to work. I need to stay with classic asp for this. Here are some things I tried: 'CDONTS Call msg.SetLocaleIDs(65001)
2
2493
by: ME | last post by:
How would one obtain the parameter VALUES of a method that has already run? I can find the method using the StackTrace and StackFrame classes but once I find the method I would like to obtain the value of one of the parameters that were passed to the method. Is this possible? Even if I have to use PInvoke to do so? I can currently find the parameter TYPEs using reflection but I can not figure out to obtain the values. Thanks,
4
6875
by: =?ISO-8859-2?Q?Boris_Du=B9ek?= | last post by:
Hi, I have an API that returns UTF-8 encoded strings. I have a utf8 codevt facet available to do the conversion from UTF-8 to wchar_t encoding defined by the platform. I have no trouble converting when a UTF-8 encoded string comes from file - I just create a std::wifstream and imbue it with a locale that uses the utf-8 facet for std::locale::ctype. Then I just use operator>to get wstring properly decoded from UTF-8. I thought I could...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.