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;
{
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;
在中文系统下,ANSI编码可能是GB2312。
在日文系统下,ANSI编码可能是JIS。如果你要用GB2312,就直接写_doc.CreateXmlDeclaration("1.0", "gb2312", "");
不过,建议还是用UTF-8或UTF-16,等常用编码。
这个就会自动LS所说的系统设置的语言了
然后生成的文件要另存为ANSI。
XmlDeclaration xmldecl = _doc.CreateXmlDeclaration("1.0", "ASCII", ""); 这样的话中文内容好像显示不出来呢。
我自己找了一个方法:在save后加上System.IO.File.WriteAllText(filepath, System.IO.File.ReadAllText(filepath, Encoding.UTF8), Encoding.Default);
{
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...)
}