1

I have a list of xmldocuments converted from C# objects using Newtonsoft.Json API. I need help in merging/combining and saving the list to a file.

My code looks like below

 var xmlDocumentsList = new List<XmlDocument>();
       foreach (var user in users)
        {
           xmlDocumentsList.Add(JsonConvert.DeserializeXmlNode(JsonConvert.SerializeObject(user),"user"));                
        }

I have looked into XmlTextWriter class examples but could not find an example merging the objects and saving them.

The xmlDocumentsList has user items like below

<user>  
 <first_name>Test1</first_name>
 <last_name>Test1</last_name>   
</user>

<user>  
 <first_name>Test2</first_name>
 <last_name>Test2</last_name>   
</user>

and I am looking the combined file to be like

<users>
<user>  
    <first_name>Test1</first_name>
    <last_name>Test1</last_name>    
</user>

<user>  
    <first_name>Test2</first_name>
    <last_name>Test2</last_name>    
</user>
</users>
rumi
  • 3,293
  • 12
  • 68
  • 109
  • Do you have to save each `user` as its own xml document? Are you able to serialize the whole array of `users`? – Mike_G Mar 23 '17 at 12:31
  • I want to combine all the users in a single file. – rumi Mar 23 '17 at 12:39
  • You shouldn't need to jump through all those hoops to convert the object to XML. I would take a look at this answer: http://stackoverflow.com/a/16853268/52051 Then once you have a string, you can call File.Write – Mike_G Mar 23 '17 at 13:00

0 Answers0