大家好,目前我已将所有 xml 文件保存在一个文件中。但我需要保存在单独的文件中。你能帮我吗?
当前代码:-
System.Data.DataTable checkFile = new System.Data.DataTable();
sql = "sql";
DBCon clsDB = new DBCon(SAPCompanySettings.MSSQLConnectionString);
clsDB.Execute(sql, ref checkFile);
var stringwriter = new StringWriter();
using (var xmlWriter = XmlWriter.Create(stringwriter, new XmlWriterSettings { Indent = true }))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Root");
var groups = checkFile.AsEnumerable().GroupBy(x => new { DocNum = x.Field<int>("orderid"), Type = x.Field<string>("Type"), Status = x.Field<string>("Status"), productno = x.Field<string>("productno"), ProductDescription = x.Field<string>("ProductDescription"), uom = x.Field<string>("uom"), customer = x.Field<string>("customer"), remark = x.Field<string>("remark"), U_JobNumber = x.Field<string>("U_JobNumber"), U_SalesPerson = x.Field<string>("U_SalesPerson"), U_JobType = x.Field<string>("U_JobType"), PlannedQty = x.Field<decimal>("PlannedQty"), OriginNum = x.Field<int?>("OriginNum"), orderdate = x.Field<DateTime>("orderdate"), duedate = x.Field<DateTime>("duedate") });
foreach (var group in groups)
{
xmlWriter.WriteStartElement("productionorder");
xmlWriter.WriteAttributeString("orderid", group.Key.DocNum.ToString());
xmlWriter.WriteElementString("type", group.Key.Type);
xmlWriter.WriteElementString("status", group.Key.Status);
xmlWriter.WriteElementString("productno", group.Key.productno); xmlWriter.WriteElementString("ProductDescription", group.Key.ProductDescription);
xmlWriter.WriteElementString("PlannedQty",group.Key.PlannedQty.ToString());
xmlWriter.WriteElementString("uom", group.Key.uom);
xmlWriter.WriteElementString("salesorder", group.Key.OriginNum.ToString()); xmlWriter.WriteElementString("customer", group.Key.customer);
xmlWriter.WriteElementString("Remarks", group.Key.remark);
xmlWriter.WriteStartElement("date");
xmlWriter.WriteElementString("orderdate", group.Key.orderdate.ToString());
xmlWriter.WriteElementString("duedate", group.Key.duedate.ToString());
xmlWriter.WriteEndElement();
xmlWriter.WriteElementString("jobdescription", group.Key.U_JobNumber);
xmlWriter.WriteElementString("salesperson", group.Key.U_SalesPerson);
xmlWriter.WriteElementString("jobtype", group.Key.U_JobType);
foreach (DataRow row in group)
{
xmlWriter.WriteStartElement("Components");
xmlWriter.WriteElementString("itemno", row.Field<string>("itemno"));
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement();
var sql2 = "update IS_XML_generated='Y' from OWOR where DocNum=group.Key.DocNum";
clsDB.Execute(sql2);
}
xmlWriter.WriteEndElement();
}
var xml = stringwriter.ToString();
XmlDocument docSave = new XmlDocument();
docSave.LoadXml(stringwriter.ToString());
docSave.Save(System.IO.Path.Combine(@"C:\DEMO PSL\DOXMLFILE", "Filename.xml"));
电流输出:-
filename.xml:-两个订单都保存在同一个文件中
<?xml version="1.0" encoding="utf-16"?>
<Root>
<productionorder orderid="1">
...................
</productionorder>
<productionorder orderid="2">
...............
</productionorder>
</Root>
但预期的输出:-我需要保存不同的不同文件 1.xml:-
<?xml version="1.0" encoding="utf-16"?>
<Root>
<productionorder orderid="1">
..............
</productionorder>
</Root>
2.xml:-
<?xml version="1.0" encoding="utf-16"?>
<Root>
<productionorder orderid="2">
............
</productionorder>
</Root>