Results 1 to 4 of 4

Thread: [RESOLVED] Merge multiple XML files to a single XML file.

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved [RESOLVED] Merge multiple XML files to a single XML file.

    I would like to find an example using Linq to Xml that will merge n number of Xml Files.

    I already have this code from MSDN that does this, but it doesn't use Linq.

    C# Code:
    1. try
    2.     {
    3.         XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books1.xml");
    4.         XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books2.xml");
    5.  
    6.         DataSet ds = new DataSet();
    7.         ds.ReadXml(xmlreader1);
    8.         DataSet ds2 = new DataSet();
    9.         ds2.ReadXml(xmlreader2);
    10.         ds.Merge(ds2);
    11.         ds.WriteXml("C:\\Books.xml");
    12.         Console.WriteLine("Completed merging XML documents");
    13.     }
    14.     catch (System.Exception ex)
    15.     {
    16.         Console.Write(ex.Message);
    17.     }
    18. Console.Read();

    I want to check if Linq is faster than the above method.
    I have something along the lines of this code snippet.

    C# Code:
    1. var xml1 = XDocument.Load("C:\\Books1.xml");
    2. var xml2 = XDocument.Load("C:\\Books2.xml");
    3.  
    4. //Combine and remove duplicates
    5. var combined = xml1.Descendants("AllNodes")
    6.                           .Union(xml2.Descendants("AllNodes"));

    combined is of type System.Linq.Enumerable.UnionIterator<System.Xml.Linq.XElement>

    How do I serialize it to an XML file?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Merge multiple XML files to a single XML file.

    Might be a little off without having some sample xml documents to work with.

    Code:
                var xml1 = XDocument.Load("C:\Temp\Books1.xml");
                var xml2 = XDocument.Load("C:\Temp\Books2.xml");
    
                xml1.Descendants("AllBooks").FirstOrDefault()
                    .Add(xml2.Descendants("AllBooks").FirstOrDefault().Nodes());
    
                xml1.Save("C:\Temp\CombinedBooks.xml");
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Merge multiple XML files to a single XML file.

    Hi MattP,
    Thanks for the response. Firstly I apologize that I did not post the files in question. Both files have root elements.
    So the structure looks something like this.

    PHP Code:
    <Root>
        <
    Books>
        <
    Book Id="AAHAH">
        <
    TITLE>Paths of Glory</TITLE>
        <
    AUTHOR>Jeffrey Archer</AUTHOR>
        <
    YEAR>2010</YEAR>
        </
    Book>
        </
    Books>
    </
    Root
    I realize that your method will not work for me. I will have to do something along the lines of selecting elements from both xml files and then merging them.

    Regards,
    Abhijit
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Merge multiple XML files to a single XML file.

    Maybe I'm not understanding you correctly.

    Here's my Books1.xml that I used:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Root>
    	<Books>
    		<Book Id="Book1">
    			<TITLE>Title of Book 1</TITLE>
    			<AUTHOR>Author of Book 1</AUTHOR>
    			<YEAR>2010</YEAR>
    		</Book>
    		<Book Id="Book2">
    			<TITLE>Title of Book 2</TITLE>
    			<AUTHOR>Author of Book 2</AUTHOR>
    			<YEAR>2011</YEAR>
    		</Book>
    	</Books>
    </Root>
    And Books2.xml:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Root>
    	<Books>
    		<Book Id="Book3">
    			<TITLE>Title of Book 3</TITLE>
    			<AUTHOR>Author of Book 3</AUTHOR>
    			<YEAR>2006</YEAR>
    		</Book>
    		<Book Id="Book4">
    			<TITLE>Title of Book 4</TITLE>
    			<AUTHOR>Author of Book 4</AUTHOR>
    			<YEAR>2000</YEAR>
    		</Book>
    	</Books>
    </Root>
    Here's the code I used to test with:

    Code:
                var xml1 = XDocument.Load("C:\\Temp\\Books1.xml");
                var xml2 = XDocument.Load("C:\\Temp\\Books2.xml");
    
                xml1.Descendants("Books").FirstOrDefault().Add(xml2.Descendants("Books").FirstOrDefault().Nodes());
    
                xml1.Save("C:\\Temp\\CombinedBooks.xml");
    And here's the resultant CombinedBooks.xml:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Root>
      <Books>
        <Book Id="Book1">
          <TITLE>Title of Book 1</TITLE>
          <AUTHOR>Author of Book 1</AUTHOR>
          <YEAR>2010</YEAR>
        </Book>
        <Book Id="Book2">
          <TITLE>Title of Book 2</TITLE>
          <AUTHOR>Author of Book 2</AUTHOR>
          <YEAR>2011</YEAR>
        </Book>
        <Book Id="Book3">
          <TITLE>Title of Book 3</TITLE>
          <AUTHOR>Author of Book 3</AUTHOR>
          <YEAR>2006</YEAR>
        </Book>
        <Book Id="Book4">
          <TITLE>Title of Book 4</TITLE>
          <AUTHOR>Author of Book 4</AUTHOR>
          <YEAR>2000</YEAR>
        </Book>
      </Books>
    </Root>
    You say you've got n number of files to go through. Just keep adding the nodes to xml1.Descendants("Books").FirstOrDefault() and then save the merged files when you're done.

    Code:
                var xml1 = XDocument.Load(@"C:\Temp\Books1.xml");
                var xml2 = XDocument.Load(@"C:\Temp\Books2.xml");
    
                xml1.Descendants("Books").FirstOrDefault().Add(xml2.Descendants("Books").FirstOrDefault().Nodes());
    
                var xml3 = XDocument.Load(@"C:\Temp\Books3.xml");
    
                xml1.Descendants("Books").FirstOrDefault().Add(xml3.Descendants("Books").FirstOrDefault().Nodes());
    
                xml1.Save("C:\\Temp\\CombinedBooks.xml");
    If you're looking for something different then I'll need a little more information on what you'd like the end result to look like before I can help.
    Last edited by MattP; Aug 2nd, 2012 at 03:52 PM.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width