473,289 Members | 2,091 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,289 software developers and data experts.

Encoding

1. How do I determine which encoding a xmldocument or xmlreader uses when
opening a document?
I'm not just talking about the <?xml encoding="utf-8"?attribute, but the
actual encoding of the characters in the underlying stream.

2. How do I make sure that the encoding of my created xmldocument or
xmlwriter is in utf-8?

Thanks!
/mortb
Feb 2 '07 #1
3 5455
* mortb wrote in microsoft.public.dotnet.xml:
>1. How do I determine which encoding a xmldocument or xmlreader uses when
opening a document?
I'm not just talking about the <?xml encoding="utf-8"?attribute, but the
actual encoding of the characters in the underlying stream.
I am not aware of a property that exposes this information. Note that if
the XML declaration is absent or has no encoding declaration, the actual
encoding is generally assumed to be UTF-8 (exceptions being e.g. if the
document is loaded through some higher level protocol that specifies
other encoding information), and cases where you actually need to know
are very rare.
>2. How do I make sure that the encoding of my created xmldocument or
xmlwriter is in utf-8?
XmlDocument objects are in no character encoding, they only are after
you have serialized them to some byte string like when writing to a
file. As for the various serialization methods, they generally either
have an option to specify the encoding or inherit it in some way. For
example, XmlTextWriter has a constructor that accepts a System.Text.
Encoding object.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Feb 2 '07 #2

"Bjoern Hoehrmann" <bj****@hoehrmann.dewrote in message
news:h9********************************@hive.bjoer n.hoehrmann.de...
>
I am not aware of a property that exposes this information. Note that if
the XML declaration is absent or has no encoding declaration, the actual
encoding is generally assumed to be UTF-8 (exceptions being e.g. if the
document is loaded through some higher level protocol that specifies
other encoding information), and cases where you actually need to know
are very rare.
The reason I need to know is that there will be text in Asian languages
(e.g. Chineese) in the XML file. In this case encoding is important.
>
>>2. How do I make sure that the encoding of my created xmldocument or
xmlwriter is in utf-8?

XmlDocument objects are in no character encoding, they only are after
you have serialized them to some byte string like when writing to a
file. As for the various serialization methods, they generally either
have an option to specify the encoding or inherit it in some way. For
example, XmlTextWriter has a constructor that accepts a System.Text.
Encoding object.
I'll use this constructor then. Thanks!

cheers,
mortb
Feb 2 '07 #3
mortb wrote:
1. How do I determine which encoding a xmldocument or xmlreader uses when
opening a document?
I'm not just talking about the <?xml encoding="utf-8"?attribute, but the
actual encoding of the characters in the underlying stream.
I don't understand that question. If the encoding is delcared in the XML
declaration (e.g. <?xml version="1.0" encoding="UTF-8"?>) then the
characters in the underlying stream have to encoded with UTF-8,
otherwise the document is not well-formed.
As for reading out the encoding in the XML declaration, XmlReader allows
it with e.g. NET 2.0 C#

using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
if (xmlReader.Read() && xmlReader.NodeType ==
XmlNodeType.XmlDeclaration) {
Console.WriteLine("Declared encoding is: {0}.",
xmlReader.GetAttribute("encoding"));
}
else {
Console.WriteLine("No XML declaration found.");
}
}
That obviously only helps if the XML document has an XML declaration
declaring the encoding. If not the XML is either UTF-8 or UTF-16
encoded, depending on the presence and form of a byte order mark at the
very beginning.

If you are not working with XmlReader but with XmlDocument then you will
also find the XmlDeclaration node in the DOM tree e.g.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
if (xmlDocument.FirstChild.NodeType ==
XmlNodeType.XmlDeclaration) {
XmlDeclaration declaration = xmlDocument.FirstChild as
XmlDeclaration;
Console.WriteLine("Declared encoding is: {0}.",
declaration.Encoding);
}
else {
Console.WriteLine("No XML declaration found.");
}
2. How do I make sure that the encoding of my created xmldocument or
xmlwriter is in utf-8?
As already pointed out, you can specify the encoding to use when
creating the XmlWriter e.g. in .NET 2.0 with the XmlWriterSettings e.g.
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Encoding = System.Text.Encoding.UTF8;
using (XmlWriter xmlWriter = XmlWriter.Create(@"file.xml",
writerSettings) {
xmlWriter.WriteStartDocument();
...
}

But UTF-8 is pretty much the default, for instance when the Save method
of an XmlDocument method is being called and there is no XML declaration
or there is one but no encoding is specified then UTF-8 is used.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 2 '07 #4

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

Similar topics

10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
8
by: davisjoseph | last post by:
Hi All, I'm newbie to this XML world. My problem is to identify the encoding type of XML at runtime. What currently I'm doing is checking whether BOM is available in the XML; based on the BOM...
8
by: Demon News | last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the Transform I'm specifying the the output xsl below: <xsl:output method="xml" encoding="UTF-8" indent="no"/> the...
4
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
2
by: velle | last post by:
My headache is growing while playing arround with unicode in Python, please help this novice. I have chosen to divide my problem into a few questions. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) ...
0
by: Chris McDonough | last post by:
ElementTree's XML serialization routine implied by tree._write(file, node, encoding, namespaces looks like this (elided): def _write(self, file, node, encoding, namespaces): # write XML to file...
4
by: Bob | last post by:
Hi Need to produce a Doc with no encoding info. Is there anyway of doing this? Thanks Bob i.e. <?xml version=\"1.0\" ?>
4
by: Christina | last post by:
Hey Guys, Currently, I am using the below code: Dim oReqDoc as XmlDocument Dim requiredBytes As Byte() requiredBytes = System.Text.UTF8Encoding.UTF8.GetBytes(oReqDoc.InnerXml). Here, I am...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.