XML encoding issue

This is a discussion on XML encoding issue within the C# Programming forums, part of the General Programming Boards category; Hello everyone, Here is my code, and it will always output UTF-16 at XML header even if I set the ...

  1. #1
    Registered User
    Join Date
    May 2006

    XML encoding issue

    Hello everyone,

    Here is my code, and it will always output UTF-16 at XML header even if I set the XML declaration to UTF-8.

    Here is my code and output.

    My questions,

    1. How to make UTF-8 in header other than UTF-16?
    2. Is the XML string really UTF-16 encoded or UTF-8 encoded? I think in C#, string is always UTF-16 encoded, why do we need a UTF-8 in header?

    <?xml version="1.0" encoding="utf-16"?>
    <CategoryList a="12345" b="1d5458cd-a070-40cc-a3f4-cf3c394013cc" c="true" />
    using System;
    using System.Text;
    using System.IO;
    using System.Xml;
    class Test
        public static void Main()
            XmlDocument xmlDoc = new XmlDocument();
            // Write down the XML declaration
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            // Create the root element
            XmlElement rootNode = xmlDoc.CreateElement("CategoryList");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            // Set attribute name and value!
            rootNode.SetAttribute("a", "12345");
            rootNode.SetAttribute("b", Guid.NewGuid().ToString());
            rootNode.SetAttribute("c", "true");
            // Save to the XML file
            StringWriter stream = new StringWriter();
            string content = stream.ToString();

    thanks in advance,

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    I usually let the xml writer handle the encoding itself:
    			System.Xml.XmlTextWriter Writer = null;
    				Writer = new System.Xml.XmlTextWriter(FileName, System.Text.Encoding.UTF8);
    				Writer.Formatting = System.Xml.Formatting.Indented;
    				System.Xml.XmlDocument Document = new System.Xml.XmlDocument();
    				System.Xml.XmlNode RootNode = Document.CreateElement(RootTagName);
    				SaveXml(Document, RootNode);
    				if(Writer != null) Writer.Close();

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. html encoding issue
    By Checker1977 in forum Tech Board
    Replies: 8
    Last Post: 12-18-2008, 05:18 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  3. XML and data exchange
    By seexml in forum C Programming
    Replies: 0
    Last Post: 04-27-2006, 04:02 PM
  4. Need help with this xml tree example
    By kzar in forum C Programming
    Replies: 1
    Last Post: 11-22-2004, 11:23 AM
  5. encoding problems with xml (MSXML3.0)
    By Pieter in forum Windows Programming
    Replies: 0
    Last Post: 08-19-2002, 03:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21