473,769 Members | 6,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5494
* mortb wrote in microsoft.publi c.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****@h oehrmann.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****@hoehrma nn.dewrote in message
news:h9******** *************** *********@hive. bjoern.hoehrman n.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.Creat e(@"file.xml") ) {
if (xmlReader.Read () && xmlReader.NodeT ype ==
XmlNodeType.Xml Declaration) {
Console.WriteLi ne("Declared encoding is: {0}.",
xmlReader.GetAt tribute("encodi ng"));
}
else {
Console.WriteLi ne("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.Loa d(@"file.xml") ;
if (xmlDocument.Fi rstChild.NodeTy pe ==
XmlNodeType.Xml Declaration) {
XmlDeclaration declaration = xmlDocument.Fir stChild as
XmlDeclaration;
Console.WriteLi ne("Declared encoding is: {0}.",
declaration.Enc oding);
}
else {
Console.WriteLi ne("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 XmlWriterSettin gs e.g.
XmlWriterSettin gs writerSettings = new XmlWriterSettin gs();
writerSettings. Encoding = System.Text.Enc oding.UTF8;
using (XmlWriter xmlWriter = XmlWriter.Creat e(@"file.xml",
writerSettings) {
xmlWriter.Write StartDocument() ;
...
}

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
687
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 codepage system wide. As much as it sounds good, UTF/Unicode encoding is not an option, nor is changing the codepage. Tackling the first is easy via Encoding.Default.GetBytes and shoving it over the network. However, Encoding.Default is the...
8
2496
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 I'm identifying the encoding type. Here is the problem, some type of UTF-8 encoded file does'nt have BOM in the starting. So I'm identying the file as iso-8859-1 encoded which is actually encoded in UTF-8. I dont have much idea about the...
8
607
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 resulting xml ends up with the following declaration: <?xml version="1.0" encoding="utf-16"?> changing the encoding to utf-16, is there something I'm doing wrong? Is it
4
8848
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 version="1.0" encoding="utf-8" ?>... - ... I am using the WebRequest functionality to get the feeds. So, my code
2
5071
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) on linux2 1) Does " >>>print 'hello' " simply write to sys.stdout?
0
1972
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 tag = node.tag if tag is Comment: file.write("<!-- %s -->" % _escape_cdata(node.text, encoding)) elif tag is ProcessingInstruction: file.write("<?%s?>" % _escape_cdata(node.text, encoding))
4
5562
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
8376
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 encoding my xml string in UTF8 format.
1
32947
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); ###Question: Now this creates finalfile.txt with ANSI Encoding ...which is a default. Either tell me how to change the default or how to create a
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
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
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.