记录列表中有一些记录但是当我调用以下方法时,它不会将数据写入xml。它只是写作。 我是XML的新手。请帮帮我。
public void SaveRentalRecords()
{
// create the XmlWriterSettings object
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
// create the XmlWriter object
XmlWriter xmlOut = XmlWriter.Create(path, settings);
// write the start of the document
xmlOut.WriteStartDocument();
xmlOut.WriteStartElement("RentalRecords");
// write each Product object to the xml file
foreach (RecordList record in Records)
{
xmlOut.WriteStartElement("RentalRecord");
xmlOut.WriteElementString("TenantID", record.TenantID);
xmlOut.WriteElementString("TenantName", record.TenantName);
xmlOut.WriteElementString("PropertyID", record.PropertyID);
xmlOut.WriteElementString("PropertyAddress", record.PropertyAddress);
xmlOut.WriteEndElement();
MessageBox.Show(record.TenantID+record.TenantName+record.PropertyID+record.PropertyAddress);
}
// write the end tag for the root element
xmlOut.WriteEndElement();
// close the XmlWriter object
xmlOut.Close();
}
答案 0 :(得分:1)
您可以尝试以下方法:
var result = new XElement("RentalRecords", new XElement("RentalRecord", recs.Select(x => new XElement(x.tenantId.ToString(CultureInfo.InvariantCulture), x.tenantName, x.PropertyId.ToString(CultureInfo.InvariantCulture), x.PropertyName))));
result.Save("RentalRecords.xml");