Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

I am new to C# and enjoying learning the language
I have written the below code in C# to deserialize a file:
C#
XmlSerializer deserializer = new XmlSerializer(typeof(CARS), new XmlRootAttribute("CARS"));
    using (XmlReader reader = XmlReader.Create(@"C:\carsFile1.xml")) 
    {
        xmlReadIn = (CARS)deserializer.Deserialize(reader);
    }
    Console.WriteLine("File 1 Successfully read");            
    return xmlReadIn;


The code works fine for 1 file what I want to do is deserialize 2 files and merge them into 1 XML document.
Both files have identical fields but 1 file has more data inside it then other.
Its important to avoid duplicating data and just append to the file and not duplicate records already in file.
So I would like to append carsFile1 + carsFile2 into one file, carsFinal
How can I adapt my code to do this?
Thanks in advance
Posted
Updated 16-Jun-14 23:03pm
v3
Comments
Richard MacCutchan 18-Jun-14 5:01am    
You need to deserialize each file into its own CARS object and then do a merge/sort of the records in each object to get the final result.

1 solution

Thanks for your reply it took some going over.

What I did was, read in both the deserialized XML files and stored them into objects, used those objects to store into LISTS, created an empty list to store the unique records into, iterated through both lists (fl and fl1) to find unique values and store any unique values into the empty list and then finally returned the list as object to use elsewhere.

It took some going over but got there in the end....



public CARS getUniqueFlightRecords()
        {
            //create instance variables of class DeserializeXML
            DeserializeXML file1 = new DeserializeXML();
            DeserializeXML file2 = new DeserializeXML();

            //store both deserialized files into individual CARS objects 
            CARS xmlFile1 = file1.deserializeFileOne();
            CARS xmlFile2 = file2.deserializeFileTwo();                        

            //store each object from above into lists to iterate through to find unique records
            List<cars> fl = xmlFile1.CARS.ToList();
            List<cars> fl1 = xmlFile2.CARS.ToList();

            //create a new list to store any unique car records you find
            List<cars> flx = new List<cars>();

            //iterate through both lists to find unique items and store them in empty list
            flx.AddRange(fl1);
            
            foreach (var item in fl)
            {
                string carNum= item.CarID.Trim() + item.ManuCode.Trim();
                if (!fl1.Any(x => string.Equals(x.CarID.Trim() + x.ManuCode.Trim(), carNum)))
                {
                    flx.Add(item);
                }
            }
                        
            CARS vehicle = new CARS();
            vehicle.CARS = flx.ToArray();
            vehicle.CURRENT_DATE = DateTime.Now.ToString();

            //return car object
            return vehicle;
        }


There are other more easier ways of doing this like merging both files into 1 file which will contain duplicates and iterate through that list only and store unique records into an empty list

Thanks anyway I only put the solution up for anyone who may need to do this kind of thing.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900