Encoding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mortb

    #1

    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


  • Bjoern Hoehrmann

    #2
    Re: Encoding

    * 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:bjoern@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/

    Comment

    • mortb

      #3
      Re: Encoding


      "Bjoern Hoehrmann" <bjoern@hoehrma nn.dewrote in message
      news:h9t5s2h2t8 mc7qkjb4ju9s3oo tsia70lcc@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


      Comment

      • Martin Honnen

        #4
        Re: Encoding

        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/

        Comment

        Working...