先上代码
 public override string SerializeToXml(object objArg)
        {
            if (objArg == null)
                return "";            StringBuilder builder = new StringBuilder();
            try
            {
                //声明Xmldocument对象
                _docXml = new XmlDocument();                //处理描述信息
                _docXml.AppendChild(_docXml.CreateXmlDeclaration("1.0", "utf-8", "yes"));                //序列化对象
                DoSirialAtFirst(objArg, _docXml);                //写入到Builder对象
                XmlWriter writer = XmlWriter.Create(builder);                 //保存
                _docXml.Save(writer);
            }
            catch (Exception ex)
            {
                throw new Exception("客户端序列化对象出错!",ex);
            }
            return builder.ToString();
        }
红色区域设置怎么无效啊,还有msdn上面说如果不进行encoding的设置,默认utf-8,为什么我设置不设置都是utf-16郁闷,因为要和java交互,所以必须用utf-8,汗~

解决方案 »

  1.   

    XmlDocument.InsertBefore();XmlDocument xmldoc =new ...
    XmlDeclicration dec = xmldoc.CreateXmlDeclaration("1.0","utf-8",null);xmldoc.AppendChildren(dec);顺便看看你的xml路径是否你操作的xml路径。
    我以前遇见过类似情况、
      

  2.   

    _docXml.AppendChild(_docXml.CreateXmlDeclaration("1.0", null, null));  
      

  3.   

    http://social.microsoft.com/Forums/zh-CN/xmlwebserviceszhchs/thread/07c39f40-f8c6-4450-929b-73923796230c
      

  4.   


    得到的结果还是不对,总是utf-16
      

  5.   


    我是新创建的一个XmlDocument对象,然后创建好节点后保存在StringBuilder中的,直接对StringBuilder进行监视,所以不存在路径问题
      

  6.   

    _docXml.CreateXmlDeclaration("1.0", "utf-8", "yes"),只是生成一个说明,并不决定保存到磁盘上的文件是UTF_8的格式,需要用StreamWrite 控制保存的格式。
      

  7.   

    // Create an XML declaration. 
        XmlDeclaration xmldecl;
        xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
        xmldecl.Encoding="UTF-8";
        xmldecl.Standalone="yes";     
      

  8.   

    XmlDocument doc = new XmlDocument();
      XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null);
      doc.AppendChild(dec);
      XmlElement root = doc.CreateElement("First");
      doc.AppendChild(root);
      XmlNode node = doc.CreateElement("Seconde");
      XmlElement element1 = doc.CreateElement("Third1");
      element1.SetAttribute("Name", "A");
      element1.SetAttribute("ID", "1");
      element1.InnerText = "";
      node.AppendChild(element1);  XmlElement element2 = doc.CreateElement("Third2");
      element2.SetAttribute("Name", "B");
      element2.SetAttribute("ID", "2");
      element2.InnerText = "";
      node.AppendChild(element2);
        
      root.AppendChild(node);
      doc.Save(@"d:\bb.xml");
      Console.Write(doc.OuterXml);