简体   繁体   中英

How to merge multiple XML files with data in same format in it to a single XML file with all data together using C#.NET?

I have a blob (XYZ) that receives multiple XML files in same format. See below

XML file 1:

<Product>
    <ID>001</ID>
    <Name>John</Name>
    <Designation>Developer</Designation>
</Product>

XML file 2:

<Product>
    <ID>002</ID>
    <Name>Peter</Name>
    <Designation>Tester</Designation>
</Product>

XML file 3:

<Product>
    <ID>003</ID>
    <Name>Arun</Name>
    <Designation>Support</Designation>
</Product>

XML file 4:

<Product>
    <ID>004</ID>
    <Name>Swetha</Name>
    <Designation>Analyst</Designation>
</Product>

XML file 5:

<Product>
    <ID>005</ID>
    <Name>Gokul</Name>
    <Designation>Maintainence</Designation>
</Product>

I need to merge all these files to a single XML file like below and put into another blob (ABC).

Merged XML file:

<xml>
<Product>
    <ID>001</ID>
    <Name>John</Name>
    <Designation>Developer</Designation>
</Product>

<Product>
    <ID>002</ID>
    <Name>Peter</Name>
    <Designation>Tester</Designation>
</Product>

<Product>
    <ID>003</ID>
    <Name>Arun</Name>
    <Designation>Support</Designation>
</Product>

<Product>
    <ID>004</ID>
    <Name>Swetha</Name>
    <Designation>Analyst</Designation>
</Product>

<Product>
    <ID>005</ID>
    <Name>Gokul</Name>
    <Designation>Maintainence</Designation>
</Product>
</xml>

I may need one like this.

What I have tried so far is below one.

                    using (var jw = new XmlTextWriter(sw)) //sw holds the o/p location to store the merged files 
                    {
                        jw.WriteStartElement("root");

                        int i = 0;
                        int c = list.Count();
                        foreach (var item in list)
                        {
                            if (i > 0)
                                await jw.WriteRawAsync("\r\n");

                            var blobdata = await OutputContainerService.GetContentAsync(input.InputLocation + "/" + item);
                            await jw.WriteRawAsync(blobdata);
                            i++;
                        }

                        jw.WriteEndElement();

                        await jw.FlushAsync();
                    }

How could I achieve this using C#.NET?

Option 1 :

  1. Create Product Class
  2. Populate the Product object with values by de-serializing the xml
  3. Add the product object to List
  4. Serialize the List

Option 2:

  1. read the xml to XML Document
  2. populate the product xml as xml node
  3. Add the xml nodes to xml documents

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