【发布时间】:2017-10-30 09:14:10
【问题描述】:
我已将 xml 文件加载到 datagridview。在用户完成更改后,我必须将其保存在与加载文件完全相同的结构中。
每个文件可以包含多个 TimeSlots
<?xml version="1.0" encoding="iso-8859-1"?>
<ELO2BDE>
<TimeSlots>
<TimeSlot>
<StartTime xmlns="TEST:TEST">2017-10-10T00:00:00</StartTime>
<EndTime xmlns="TEST:TEST">2017-10-10T03:00:00</EndTime>
<WorkShift xmlns="TEST:TEST">Night Shift</WorkShift>
<WorkShiftModel xmlns="TEST:TEST">Default</WorkShiftModel>
<UtilizationCategory xmlns="TEST:TEST">Corrective Work</UtilizationCategory>
<UtilizationCategoryDetail xmlns="TEST:TEST" />
<Remarks xmlns="TEST:TEST" />
<OperatorPerson xmlns="TEST:TEST">PERSON</OperatorPerson>
<IsAutoGenerated xmlns="TEST:TEST">false</IsAutoGenerated>
<UsedEquipmentTypes xmlns="TEST:TEST" />
<NameValuePairs xmlns="TEST:TEST">
<NameValuePair>
<Name>Project Number</Name>
<Value>qwer</Value>
</NameValuePair>
<NameValuePair>
<Name>Project Sub Number</Name>
<Value>456</Value>
</NameValuePair>
<NameValuePair>
<Name>Project Fuel Sub Number</Name>
<Value>456</Value>
</NameValuePair>
<NameValuePair>
<Name>Result Series</Name>
<Value>test</Value>
</NameValuePair>
<NameValuePair>
<Name>Result</Name>
<Value>435-dfg-345</Value>
</NameValuePair>
<NameValuePair>
<Name>Version</Name>
<Value>1</Value>
</NameValuePair>
<NameValuePair>
<Name>Engine Running Time</Name>
<Value>390.4911</Value>
</NameValuePair>
<NameValuePair>
<Name>Cum Fuel Consumption</Name>
<Value>0</Value>
</NameValuePair>
<NameValuePair>
<Name>P Comment</Name>
<Value />
</NameValuePair>
<NameValuePair>
<Name>Type of Test</Name>
<Value>0</Value>
</NameValuePair>
<NameValuePair>
<Name>Test Sequence Number</Name>
<Value />
</NameValuePair>
<NameValuePair>
<Name>P Operating Mode</Name>
<Value>290</Value>
</NameValuePair>
<NameValuePair>
<Name>P_VER</Name>
<Value>15</Value>
</NameValuePair>
<NameValuePair>
<Name>Testrun ID</Name>
<Value>436456745</Value>
</NameValuePair>
<NameValuePair>
<Name>Fuel Type ID</Name>
<Value>11</Value>
</NameValuePair>
<NameValuePair>
<Name>Dyno Power</Name>
<Value>687</Value>
</NameValuePair>
<NameValuePair>
<Name>Indi Channels</Name>
<Value>0</Value>
</NameValuePair>
<NameValuePair>
<Name>Testbed Type</Name>
<Value>1E+10</Value>
</NameValuePair>
<NameValuePair>
<Name>S_LOGTIM</Name>
<Value>20171010124918</Value>
</NameValuePair>
<NameValuePair>
<Name>Project TFMS</Name>
<Value/>
</NameValuePair>
</NameValuePairs>
<DataList xmlns="TEST:TEST">
<Value>UUT</Value>
<Value>Entire</Value>
<Value>0115138</Value>
</DataList>
</TimeSlot>
</TimeSlots>
<LogbookName>NAS Logbook</LogbookName>
这就是我将文件保存到 xDocument 列表中的方式:(我的问题的解决方案)
DataTable dt = new DataTable();
if (FileDataset.Count > 0) {
FileDataset.Clear();
}
dt = (DataTable)dgvData.DataSource;
var filegroup = dt.AsEnumerable().GroupBy(x => x.Field<string>("Filename")).ToList();
if (filegroup.Count > 0) {
foreach (var file in filegroup) {
string root = "ELO2BDE";
string xmlIdentification = string.Format("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><{0}><TimeSlots></TimeSlots><LogbookName></LogbookName></{0}>", root);
string ElementLogbookName = string.Empty;
string FileName = string.Empty;
XDocument outputDoc = XDocument.Parse(xmlIdentification);
XElement timeSlots = outputDoc.Descendants("TimeSlots").FirstOrDefault();
XElement LogbookNameRoot = outputDoc.Descendants("LogbookName").FirstOrDefault();
XNamespace nsattr = "urn:TEST:TEST:TEST:Public";
var groups = file.AsEnumerable().GroupBy(x => new {
Filename = x.Field<string>("FileName"),
starttime = x.Field<DateTime>("StartTime"),
endtime = x.Field<DateTime>("EndTime"),
workShift = x.Field<string>("WorkShift"),
workShiftModel = x.Field<string>("WorkShiftModel"),
utilizationCategory = x.Field<string>("UtilizationCategory"),
utilizationCategoryDetail = x.Field<string>("UtilizationCategoryDetail"),
remarks = x.Field<string>("Remarks"),
operationPerson = x.Field<string>("OperatorPerson"),
isAutoGenerated = x.Field<Boolean>("IsAutoGenerated"),
userEquipmentTypes = x.Field<string>("UsedEquipmentTypes"),
logbookName = x.Field<string>("LogbookName")
}).ToList();
foreach (var group in groups) {
XElement timeSlot = new XElement("TimeSlot");
timeSlots.Add(timeSlot);
timeSlot.Add(new XElement(nsattr + "StartTime", group.Key.starttime));
timeSlot.Add(new XElement(nsattr + "EndTime", group.Key.endtime));
timeSlot.Add(new XElement(nsattr + "WorkShift", group.Key.workShift));
timeSlot.Add(new XElement(nsattr + "WorkShiftModel", group.Key.workShiftModel));
timeSlot.Add(new XElement(nsattr + "UtilizationCategory", group.Key.utilizationCategory));
timeSlot.Add(new XElement(nsattr + "UtilizationCategoryDetail", group.Key.utilizationCategoryDetail));
timeSlot.Add(new XElement(nsattr + "Remarks", group.Key.remarks));
timeSlot.Add(new XElement(nsattr + "OperatorPerson", group.Key.operationPerson));
timeSlot.Add(new XElement(nsattr + "IsAutoGenerated", group.Key.isAutoGenerated));
timeSlot.Add(new XElement(nsattr + "UsedEquipmentTypes", group.Key.userEquipmentTypes));
timeSlot.Add(new XElement(nsattr + "NameValuePairs"));
XElement nameValuePairs = timeSlot.Element(nsattr + "NameValuePairs");
List<DataRow> NameValuePairs = group.ToList();
for (int i = 13; i < NameValuePairs[0].Table.Columns.Count - 3; i++) {
var value = NameValuePairs[0].ItemArray.GetValue(i);
if (!String.IsNullOrEmpty(value.ToString()))
nameValuePairs.Add(new XElement(nsattr + "NameValuePair", new object[] {
new XElement(nsattr + "Name" , NameValuePairs[0].Table.Columns[i].ColumnName),
//new XElement(nsattr + "Value", NameValuePairs[0].Table.Rows[0][i].ToString()),
new XElement(nsattr + "Value", value),
}));
}
timeSlot.Add(new XElement(nsattr + "DataList"));
XElement DataList = timeSlot.Element(nsattr + "DataList");
for (int i = NameValuePairs[0].Table.Columns.Count - 3; i < NameValuePairs[0].Table.Columns.Count; i++) {
var value = NameValuePairs[0].ItemArray.GetValue(i);
DataList.Add(new XElement(nsattr + "Value", value));
//DataList.Add(new XElement(nsattr + "Value", NameValuePairs[0].Table.Rows[0][i].ToString()));
}
ElementLogbookName = group.Key.logbookName;
FileName = group.Key.Filename;
}
LogbookNameRoot.Add(ElementLogbookName);
outputDoc.Add(new XComment("Editied file at: " + DateTime.Now + " from PC: " + System.Environment.MachineName + " and User: " + System.Environment.UserName + " "));
FileDataset.Add(FileName, outputDoc);
}
}
我该怎么做?
“NameValuePair”中的名称目前是我的列名,但当用户保存文件时,它们必须是“名称”。)
如何保存带有元素属性的 XML?
【问题讨论】:
-
如果您的问题中的 XML 不是全部在一行中,那么提供帮助会容易得多。
-
我编辑了xml文件。
标签: c# xml datagridview