XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0","UTF-8",null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
doc.Save("c:\\xx.xml");

解决方案 »

  1.   

    从数据库中取出来的数据那?比如姓名,如何保证他是utf-8编码的格式的啊?谢谢了,孟子E章
      

  2.   

    如果用DOM生成的话会自动转换的
      

  3.   

    例子
    string xml = "<Text><Textxml> <Name> 知道天高地厚</Name></Textxml></Text> ";
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.LoadXml(xml);
    Response.Clear();
    Response.Write (Server.HtmlEncode(doc.OuterXml));
    Response.Write ("<hr>");
    System.Xml.XmlDeclaration xmldecl;
    xmldecl = doc.CreateXmlDeclaration("1.0","UTF-8",null);
    doc.InsertBefore(xmldecl, doc.DocumentElement);
    System.Xml.XmlProcessingInstruction newPI;
    String PItext = "type='text/xsl' href='Text.xsl'";
    newPI = doc.CreateProcessingInstruction("xml-stylesheet", PItext);
    doc.InsertBefore(newPI,doc.DocumentElement);
    System.Xml.XmlNode nod = doc.SelectSingleNode("//Textxml");
    System.Xml.XmlNode newNode = doc.CreateNode(System.Xml.XmlNodeType.Element,"Content","");
    System.Xml.XmlCDataSection CData;
    CData = doc.CreateCDataSection(" 真是不知高地厚 的东西我的心情好 ");
    newNode.AppendChild(CData);
    nod.AppendChild(newNode);      
    Response.Write (Server.HtmlEncode(doc.OuterXml));
    doc.Save(Server.MapPath("ok.xml"));
      

  4.   

    我如何去确认,这个xml已经是utf-8编码的了?用IE打开能正常显示???就可以了么?