Merging 2 similar XML Files
Results 1 to 3 of 3

Thread: Merging 2 similar XML Files

  1. #1
    Senior Member codenamevirus's Avatar
    Join Date
    Jun 2005
    Location
    Faridabad, Haryana, India
    Posts
    298

    Question Merging 2 similar XML Files

    Hi

    I have 2 XML files of similar structure and I have requirement to merge them in a way, that Elements with same ID are replaced.

    For Example:

    If first file is Book1.xml with the following information:

    Code:
    <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
       </book>
       <book id="bk102">
          <author>Jeanette, Dasha</author>
          <title>Quack the Duck</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
       </book>
    </catalog>
    And Book2.xml is:

    Code:
    <?xml version="1.0"?>
    <catalog>
    <book id="bk102">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <genre>Romance</genre>
          <price>4.95</price>
       </book>
       <book id="bk103">
          <author>Vinzovskaia, Irina</author>
          <title>Piano Fort A</title>
          <genre>Romance</genre>
          <price>4.95</price>
       </book>
    </catalog>
    Then the merged file, say Book3.xml would be:

    Code:
    <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
       </book>
       <book id="bk102">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <genre>Romance</genre>
          <price>4.95</price>
       </book>
       <book id="bk103">
          <author>Vinzovskaia, Irina</author>
          <title>Piano Fort A</title>
          <genre>Romance</genre>
          <price>4.95</price>
       </book>
    </catalog>
    I have to do it in C#.

    Code Source: http://support.microsoft.com/kb/311530

    I tried doing the solution provided here but the resultant file has 2 values with the same ID.

    So, how to go about it?
    CodeNameVirus

  2. #2
    Senior Member codenamevirus's Avatar
    Join Date
    Jun 2005
    Location
    Faridabad, Haryana, India
    Posts
    298
    Hi

    Here's what I actually need to do!

    I want to merge 2 styles.xml files(obtained from 2 different docx files, while replacing values of the same type and retaining the other values).

    I am able to replace the User's Styles.xml with my Template's Style.xml, but the problem occurs when I have to mix/merge the 2 styles.xml files by replacing a few styles in the User's Document while retaining the other styles.

    For instance, my template's style.xml file has styles for following:

    1. Headings - tmpHead
    2. Sub - Headings - tmpSubHead

    And the user file 1, has the styles:

    1. Headings - usr1Head
    2. Lists - usr1List

    then the output styles.xml will have styles for:

    1. Headings - tmpHead (template style replaces user style)
    2. Lists - usr1List (retains user's style)

    PS: Adding the Sub-Heading style(tempSubHead) is optional, its good the either way.

    Another file 2 has the following styles in the styles.xml:

    1. Headings - usr2Head
    2. Sub Headings - usr2SubHead
    3. Lists - usr2Lists

    the output styles.xml will be like:

    1. Heading - tempHead
    2. Sub-Heading - tempSubHead
    3. Lists - usr2Lists

    In the end, its like some intelligent merging that requires to be done!

    So, how to go about it??
    CodeNameVirus

  3. #3
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    The first thing to do is understand the structure of the DataSets created from reading the Xml files (i.e. how the xml gets arranged into tables, columns and rows).

    Taking the example you posted earlier (since I'm a bit unsure of your docx example):

    Code:
    <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
       </book>
       <book id="bk102">
          <author>Randall, Cynthia</author>
          <title>Lover Birds</title>
          <genre>Romance</genre>
          <price>4.95</price>
       </book>
       <book id="bk103">
          <author>Vinzovskaia, Irina</author>
          <title>Piano Fort A</title>
          <genre>Romance</genre>
          <price>4.95</price>
       </book>
    </catalog>
    I would assume that reading this in will give you a dataset with a "book" table consisting of 4 columns (author, title, genre, price) and 3 rows. So figure that out first either by reading the API documentation for the thing that creates the dataset or by looking at it in the debugger or something.

    Once you've done that, you need to identify something that's unique in each of your entries, but that will be the same in both files that you are merging. In the example above, the "id" attribute is unique within a file but not unique across files. In the docx example, it might be "Heading", "Sub Heading", etc (I don't know).

    Now that you have all this information, you have a number of choices. Two of them that I can think of are subclassing DataSet and overriding the merge method or writing a utility method that takes two DataSets as arguments, and returns a merged DataSet.

    Code:
    public class MyDataSet : DataSet
    {
      // ...
      public override DataSet Merge(DataSet ds)
      {
        // do merging
      }
    }
    
    // or
    
    public static class DataSetUtils
    {
      public DataSet Merge(DataSet ds1, DataSet ds2)
      {
        DataSet retDs = ...
        // do merging
    
        return retDs;
      }
    }
    You also probably want to make sure that the rows in each table in each DataSet are sorted in the same order by whichever column is unique - this way you can more or less go from row 0 to the end of the larger DataSet without having to do anything fancy.

    There's probably much better ways of doing it, but you'd have to figure that out yourself. One possibility is instead of using a DataSet work on the xml directly using the Xml support in .NET.

    ac

Similar Threads

  1. Forensic Process and Tricks
    By Tiger Shark in forum The Security Tutorials Forum
    Replies: 3
    Last Post: January 12th, 2007, 10:44 PM
  2. Ultimate Guide to MS-DOS
    By Ennis in forum Other Tutorials Forum
    Replies: 30
    Last Post: July 15th, 2005, 01:06 AM
  3. Central Secure Logging in a Win2k Environment
    By Tiger Shark in forum The Security Tutorials Forum
    Replies: 5
    Last Post: March 4th, 2004, 05:00 PM
  4. more info on Henpeck Rodock worm
    By prodikal in forum Microsoft Security Discussions
    Replies: 0
    Last Post: October 12th, 2002, 11:00 AM
  5. A-Z Index of the Linux BASH basic commands
    By cyclops07 in forum Other Tutorials Forum
    Replies: 11
    Last Post: June 15th, 2002, 07:58 PM

Posting Permissions

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