try
            {
                XmlDocument _doc = new XmlDocument();
                XmlElement d = _doc.CreateElement("bin-data");
                _doc.AppendChild(d);
                XmlDeclaration xmldecl = _doc.CreateXmlDeclaration("1.0", "UTF-8", "");
                _doc.InsertBefore(xmldecl, d);
                for (int i = 0; i < shipdatalist.Count; i++)
                {
                    shipdata = shipdatalist[i];
                    XmlElement OSP_SHIP = _doc.CreateElement("lot-info");
                    OSP_SHIP.SetAttribute("seq", (i + 1).ToString());
                    XmlElement ship_to = _doc.CreateElement("ship-to");
                    XmlElement ship_no = _doc.CreateElement("ship-no");
                    XmlElement ship_date = _doc.CreateElement("ship-date");
                    XmlElement prod = _doc.CreateElement("prod");
                    XmlElement lot_no = _doc.CreateElement("lot-no");
                    XmlElement date_code = _doc.CreateElement("date-code");
                    XmlElement po_no = _doc.CreateElement("po-no");
                    XmlElement good_die_qty = _doc.CreateElement("good-die-qty");
                    XmlElement loss_qty1 = _doc.CreateElement("loss-qty1");
                    XmlElement close_flag = _doc.CreateElement("close-flag");
                    XmlElement inv_no = _doc.CreateElement("inv-no");
                    d.AppendChild(OSP_SHIP);
                    OSP_SHIP.AppendChild(ship_to);
                    OSP_SHIP.AppendChild(ship_no);
                    OSP_SHIP.AppendChild(ship_date);
                    OSP_SHIP.AppendChild(prod);
                    OSP_SHIP.AppendChild(lot_no);
                    OSP_SHIP.AppendChild(date_code);
                    OSP_SHIP.AppendChild(po_no);
                    OSP_SHIP.AppendChild(good_die_qty);
                    OSP_SHIP.AppendChild(loss_qty1);
                    OSP_SHIP.AppendChild(close_flag);
                    OSP_SHIP.AppendChild(inv_no);
                    ship_to.InnerText = shipdata.ship_to.ToString();
                    ship_no.InnerText = shipdata.ship_no.ToString();
                    ship_date.InnerText = shipdata.ship_date.ToString();
                    prod.InnerText = shipdata.prod.ToString();
                    lot_no.InnerText = shipdata.lot_no.ToString();
                    date_code.InnerText = shipdata.date_code.ToString();
                    po_no.InnerText = shipdata.po_no.ToString();
                    good_die_qty.InnerText = shipdata.good_die_qty.ToString();
                    loss_qty1.InnerText = shipdata.loss_qty1.ToString();
                    close_flag.InnerText = shipdata.close_flag.ToString();
                    inv_no.InnerText = shipdata.inv_no.ToString();
                    _doc.Save(filepath);
                }
                return true;

解决方案 »

  1.   

    你不是自己写好了UTF-8了吗?XmlDeclaration xmldecl = _doc.CreateXmlDeclaration("1.0", "ASCII", "");
      

  2.   

    ANSI格式不是好的交换格式。
    在中文系统下,ANSI编码可能是GB2312。
    在日文系统下,ANSI编码可能是JIS。如果你要用GB2312,就直接写_doc.CreateXmlDeclaration("1.0", "gb2312", "");
    不过,建议还是用UTF-8或UTF-16,等常用编码。
      

  3.   

    Encoding.Default
    这个就会自动LS所说的系统设置的语言了
      

  4.   

    客户要求这样写XmlDeclaration xmldecl = _doc.CreateXmlDeclaration("1.0", "UTF-8", "");
    然后生成的文件要另存为ANSI。
    XmlDeclaration xmldecl = _doc.CreateXmlDeclaration("1.0", "ASCII", ""); 这样的话中文内容好像显示不出来呢。
    我自己找了一个方法:在save后加上System.IO.File.WriteAllText(filepath, System.IO.File.ReadAllText(filepath, Encoding.UTF8), Encoding.Default); 
      

  5.   

    应该不会这是抛给客户的日常报表信息,是台湾的客户,他们那边的编码什么的不太清楚,可能他们那边是以一个字节的方式去读所以这样要求。之前其他的报表用BIG5的,这份报表不知道为毛要这种要求啊~囧。我之前的那个方法行吗,大神们?~客户还没有回复我。
      

  6.   

    假定里面有汉字,假定文件编码是台湾的BIG5,那么那种xml将是非法的xml(你存盘的文件读不回来):static void Test(string filename)
    {
        string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>繁體</root>";
        File.WriteAllText(filename, xml, Encoding.GetEncoding("BIG5"));    XmlDocument doc = new XmlDocument();
        doc.Load(filename); //<-- 将抛出异常(Invalid character in the given encoding...)
    }