0

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?

Black Pearl
  • 79
  • 1
  • 2
  • 9
  • By searching stack overflow for similar questions: https://stackoverflow.com/questions/6045010/how-can-i-merge-xml-files/6103177 – Oguz Ozgul Mar 19 '20 at 08:17
  • @OguzOzgul Thanks for sharing this. It explains to merge all elements under a single node. I wish to merge the XML nodes to a single file with each file separated by commas. – Black Pearl Mar 19 '20 at 08:23
  • But it's not valid xml? – Oguz Ozgul Mar 19 '20 at 08:26
  • Is this a database migration operation? How are you going to read and process this later? By getting and splitting by ',' ? – Oguz Ozgul Mar 19 '20 at 08:28
  • Because in that case what you need (assuming you have already read the Blob and know how to put another Blob back) is multiple string concatenation operations, nothing else. – Oguz Ozgul Mar 19 '20 at 08:29
  • It may not be a valid XML but that will be used by another operation. The XML format which I have provided is an example. There is another requirement. There may be multiple files and depending upon the requirement, only particular no of files should be merged to a single file. For example, if there are 100 files, 10 files should be merged to a single file so that final output would be 10 merged XML files. – Black Pearl Mar 19 '20 at 08:40
  • Can you please share what you have tried or implemented so far? Getting the blob etc. – Oguz Ozgul Mar 19 '20 at 08:59
  • @OguzOzgul I have added what I have tried so far in the question. – Black Pearl Mar 20 '20 at 10:30

1 Answers1

1

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
Chandu
  • 322
  • 4
  • 13