Home > database >  How to serialize full list instead of row by row
How to serialize full list instead of row by row

Time:06-11

My DataGridView is bound to a DataSource defined here:

BindingList<CompanyProfile> DataSource = new BindingList<CompanyProfile>();

where CompanyProfile class is:

public class CompanyProfile
{
    public string CompanyName { get; set; } 
    public string SiteName { get; set; }
    public string IMO { get; set; } = "Some Value";
} 

Now I want to take the entire DataSource and write an XML file containing all the items. Before, I tried this serialization:

// Iterate the datasource list, not the DataGridView.
foreach (CompanyProfile companyProfile in DataSource)
{
    CreateClientFile(
        companyProfile,
        fileName: Path.Combine(appData,
        $"{companyProfile.CompanyName}_{companyProfile.SiteName}.xml")
    );
}

where CreateClientFile uses XmlSerializer:

private void CreateClientFile(CompanyProfile companyProfile, string fileName)
{
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(CompanyProfile));
    using (var writer = new StreamWriter(fileName))
    {
        x.Serialize(writer, companyProfile);
    }
    // Open the file to view the result
    Process.Start("notepad.exe", fileName);
}

but I end up with a file for every record. How can I serialize my DataSource so that it makes one file with all the CompanyProfile records in it?

I really want to learn and grasp this concept, staarted reading about Serilization and Databinding as suggested but I get stuck.

I hope someone is willing to help me a bit further

CodePudding user response:

This will elaborate on the excellent comment by jdweng.

In the code you posted, you go through your DataSource and use XmlSerializer for each record. But, to achieve the outcome you want, where the DataSource writes a single file and has all of the records, you need to change your definition of the serializer:

XmlSerializer x = new XmlSerializer(
    typeof(BindingList<CompanyProfile>), 
    new XmlRootAttribute("root"));

The other change is easy, because you take your btnSerialize_Click method and serialize the entire List with the x.Serialize(writer, DataSource) line.

private void btnSerialize_Click(object sender, EventArgs e)
{
    var fileName = Path.Combine(
        appData,
        "CompanyProfiles.xml");

    using (var writer = new StreamWriter(fileName))
    {
        x.Serialize(writer, DataSource);
    }
    Process.Start("notepad.exe", fileName);
}

Your file now has the correct document structure because there is a root node. The data for the two CompanyProfile records comes under the root node:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CompanyProfile>
    <CompanyName>Linear Technology</CompanyName>
    <SiteName>Colorado Design Center</SiteName>
    <IMO>Some Value</IMO>
  </CompanyProfile>
  <CompanyProfile>
    <CompanyName>Analog Devices</CompanyName>
    <SiteName>1-1-2</SiteName>
    <IMO>Some Value</IMO>
  </CompanyProfile>
</root>

I hope this will assist you in your goal of learning more about serialization.

  • Related