Share via


C# Split xml file into multiple files and map

Question

Friday, November 10, 2017 1:35 PM

Hi,

I need to map following data to response fields in wcf service(below is xml).From one of the request field of wcf I am getting a huge string data.That string is a xml data(below xml after converting tags). I need to map to response field(each field as per xml node) in wcfschema by picking data based on tagname. How to pick and send as a single response from repeating record.(I need to pick the data based on node and has to be send to response field in service.) As multiple records are present i want to pick from each record and send the data as a single response from each xml record.Output should contain 4 seperate records/xml.

  

<AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address />
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>
    <place />
    <province /><AgressoQE/>

  <AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address />
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>
    <place />
    <province /><AgressoQE/>

<AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address />
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>
    <place />
    <province /><AgressoQE/>

<AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address />
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>
    <place />
    <province /><AgressoQE/>

Regards, vishal

All replies (1)

Monday, November 13, 2017 8:04 AM

Hi b vishal,

Thank you for posting here.

There is something wrong with your xml file. I do some change. Please try the code with xml below.

<Root>
    <AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>    
</AgressoQE>

<AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>    
</AgressoQE>
<AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>    
</AgressoQE>
<AgressoQE>
    <_recno>0</_recno>
    <_section>D</_section>
    <tab>A</tab>
    <address_type>1</address_type>
    <user_id>sss</user_id>
    <date_from>2017-09-18T00:00:00+01:00</date_from>
    <resource_id>1</resource_id>
    <date_to>2099-12-31T00:00:00+00:00</date_to>
    <birth_date>2000-01-01T00:00:00+00:00</birth_date>
    <client>w</client>    
</AgressoQE>  
</Root>
   static void Main(string[] args)
        {
            XDocument xml = XDocument.Load(@"New.xml"); // loading source xml
            var xmls = xml.Root.Elements().ToArray(); // split into elements

            for (int i = 0; i < xmls.Length; i++)
            {
                // write each element into different file
                using (var file = File.CreateText(string.Format("xml{0}.xml", i + 1)))
                {
                    file.Write(xmls[i].ToString());
                }
            }
        }

The pictures below are content of xml file.

xml1.xml

xml2.xml

xml3.xml

xml4.xml

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].