简体   繁体   中英

Not Writing data to xml file as expected

there are some records in the recordlist but when I am calling the following method, it is not writing the data to xml. it is just writing . I am absolutely new to XML. Please help me.

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();
        }

You can try the below approach :

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM