反序列化2个文件 [英] Deserializing 2 files

查看:106
本文介绍了反序列化2个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我是C#的新手并且喜欢学习语言

我在C#中编写了以下代码来反序列化一个文件:

 XmlSerializer deserializer =  new  XmlSerializer( typeof (CARS), new  XmlRootAttribute(  CARS)); 
使用(XmlReader reader = XmlReader.Create( @ C:\ carsFile1.xml))
{
xmlReadIn =(CARS)deserializer.Deserialize(reader);
}
Console.WriteLine( 文件1成功阅读);
return xmlReadIn;





代码适用于1文件我要做的是反序列化2个文件并将它们合并为1个XML文档。

两个文件都有相同的字段,但是1个文件里面有更多的数据。

重要的是避免重复数据,只是附加到文件而不是重复已经存在于文件中的记录。

所以我想将carsFile1 + carsFile2附加到一个文件中,carsFinal

如何调整我的代码呢?

提前谢谢

解决方案

感谢您的回复花了一些时间。



我做的是,读取反序列化的XML文件并将它们存储到对象中,使用这些对象存储到LISTS中,创建一个用于存储唯一记录的空列表,遍历两个列表(fl和fl1)以查找唯一值并将任何唯一值存储到空列表中,然后最终将列表作为对象返回到其他地方使用。



花了一些时间但最终到了那里......







 public CARS getUniqueFlightRecords()
{
//创建cl的实例变量ass DeserializeXML
DeserializeXML file1 = new DeserializeXML();
DeserializeXML file2 = new DeserializeXML();

//将反序列化的文件存储到单个CARS对象中
CARS xmlFile1 = file1.deserializeFileOne();
CARS xmlFile2 = file2.deserializeFileTwo();

//将上面的每个对象存储到列表中以迭代查找唯一记录
List< cars> fl = xmlFile1.CARS.ToList();
列表< cars> fl1 = xmlFile2.CARS.ToList();

//创建一个新列表来存储您找到的任何独特汽车记录
List< cars> flx = new List< cars>();

//遍历两个列表以查找唯一项并将它们存储在空列表中
flx.AddRange(fl1);

foreach(fl中的var项目)
{
string carNum = item.CarID.Trim()+ item.ManuCode.Trim();
if(!fl1.Any(x => string.Equals(x.CarID.Trim()+ x.ManuCode.Trim(),carNum)))
{
flx。新增项目);
}
}

CARS vehicle = new CARS();
vehicle.CARS = flx.ToArray();
vehicle.CURRENT_DATE = DateTime.Now.ToString();

//返回汽车对象
返回车辆;
}





还有其他更简单的方法可以将两个文件合并为1个文件,其中包含重复项和迭代项仅通过该列表并将唯一记录存储到空列表中



无论如何,我只是为可能需要执行此类操作的人提供解决方案。


Hi there,

I am new to C# and enjoying learning the language
I have written the below code in C# to deserialize a file:

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

解决方案

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.


这篇关于反序列化2个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆