简体   繁体   中英

Create XML file from collection list

I have a collection string with information about customers like name, gender etc. All custumers have an id.

Now i want to create a common XML file with all customers in it. Something like example below:

<custumers>
  <custumer>
      <name></name>
       <id></id>
       <etc></etc>
  </custumer>
</custumers>

The start up with no XML file is easy, I used linq to create the xml file.

For initial creation I used following code:

try
{
    var xEle = new XElement("Customers",
    from cus in cusList
        select new XElement("Customer",
           new XElement("Name", cus.Name),
           new XElement("gender", cus.gender),
           new XElement("etc", cus.etc));
}
xEle.Save(path);

But on the point if i want to update the XML file i get some problems to get it. My approach to solve it:

Iterate over all customers in list and check for all customers if the customer.id exists in the XML.
IF not: add new customer to xml  
IF yes: update values

My code so far:

var xEle = XDocument.Load(xmlfile);
foreach (cus in cusList)
try
{
    var cids = from cid in xEle.Descendants("ID")
               where Int32.Parse(xid.Element("ID").Value) == cus.ID          
               select new XElement("customer", cus.name),
                  new XElement ("gender"), cus.gender),
                  new XELement ("etc."), cus.etc) 

        );
       xEle.Save(xmlpath);
}

How about a different approach....

using System.IO;
using System.Text;
using System.Xml.Serialization;

public void Main()
{
    DataSet Custumers = new DataSet("Custumers");
    DataTable Custumer = new DataTable("Custumer");

    // Create the columns
    Custumer.Columns.Add("id", typeof(int)).Unique = true;
    Custumer.Columns.Add("name", typeof(string));
    Custumer.Columns.Add("gender", typeof(string));
    Custumer.Columns.Add("etc", typeof(string));

    // Set the primary key
    Custumer.PrimaryKey = { Custumer.Columns("id") };

    // Add table to dataset
    Custumers.Tables.Add(Custumer);
    Custumers.AcceptChanges();

    // Add a couple of rows
    Custumer.Rows.Add(1, "John", "male", "whatever");
    Custumer.Rows.Add(2, "Jane", "female", "whatever");
    Custumer.AcceptChanges();

    // Let's save this to compare to the updated version
    Custumers.WriteXml("Custumers_Original.xml");

    // Read in XML that contains an existing Custumer and adds a new one
    //  into a Clone of the Custumer table
    DataTable CustumerUpdate = Custumer.Clone;
    CustumerUpdate.ReadXml("Custumers_Update.xml");

    // Merge the clone table data to the Custumer table
    Custumer.Merge(CustumerUpdate);
    Custumer.AcceptChanges();

    Custumers.WriteXml("Custumers_Final.xml");
}

Custumers_Original.xml looks like this:

<?xml version="1.0" standalone="yes"?>
<Custumers>
    <Custumer>
        <id>1</id>
        <name>John</name>
        <gender>male</gender>
        <etc>whatever</etc>
    </Custumer>
    <Custumer>
        <id>2</id>
        <name>Jane</name>
        <gender>female</gender>
        <etc>whatever</etc>
    </Custumer>
</Custumers>

Custumers_Update.xml has this, making a change to John and adding George :

<?xml version="1.0" encoding="utf-8" ?>
<Custumers>
    <Custumer>
        <name>John</name>
        <id>1</id>
        <gender>male</gender>
        <etc>this is new</etc>
    </Custumer>
    <Custumer>
        <name>George</name>
        <id>3</id>
        <gender>male</gender>
        <etc>grandson</etc>
    </Custumer>
</Custumers>

After the merge, the Custumers_Final.xml contains this:

<?xml version="1.0" standalone="yes"?>
<Custumers>
    <Custumer>
        <id>1</id>
        <name>John</name>
        <gender>male</gender>
        <etc>this is new</etc>
    </Custumer>
    <Custumer>
        <id>2</id>
        <name>Jane</name>
        <gender>female</gender>
        <etc>whatever</etc>
    </Custumer>
    <Custumer>
        <id>3</id>
        <name>George</name>
        <gender>male</gender>
        <etc>grandson</etc>
    </Custumer>
</Custumers>

Also my solution with linq:

 try
   {
      XDocument xEle = XDocument.Load(path);
      var ids = from id in xEle.Descendants("Custom")
      where id.Element("id").Value == cus.ID
         select id;

      foreach (XElement idCustom in ids) 
      {
         idCustom.SetElementValue("NewName", "NewElement");
      }
      xEle.Save(path);
      }

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