Archived Forum Post

Index of archived forum posts

Question:

Merging XML Files, taking changes from one while preserving unmatched content

Apr 03 '13 at 22:56

I have a WCF web service that I use other (paid) Chilkat components in and have just started leveraging the free XML class.

In this service, an XML document is created during the scanning of 3 distinct systems, all of which have some unique attributes as well as several common attributes.

The basic layout of the document is:

<VirtualMachineDetails>
    <Configuration>
    </Configuration>
    <Hardware>
    </Hardware>
    <OperatingSystem>
    </OperatingSystem>    
</VirtualMachineDetails>

However, in each node, there could either be a child with a value, or a parent who has children of its own:

<Hardware>
    <Processor>
        <Count>1</Count>
    </Processor>
    <Memory>2048</Memory>
</Hardware>

Where the interesting behavior comes into play and something out of my control, depending on the state of a given VM, some values may not be populated. For example, if a VM is turned off, certain properties cannot be retrieved.

However, other attributes that are retrieved may have been updated since the last retrieval and I want to persist those changes.

To do this, I need to treat the XML I already have stored as the "master" document and merge the contents of the newly retrieved content and anywhere there is a match, overwrite and anything new, append.

I went through the example apps hoping this was a simple process that was integrated into the library but I didn't see anything. I could write it from scratch but if anyone has an example lying around it would be greatly appreciated.

Thanks,

Jim


Answer

Have you considered leveraging LinqToXML ? 3rd party components like Chilkat specialize in getting you the information, and sometimes go the extra mile with features that help with common tasks, but when you delve into areas like the one you describe (XML manipulation), you are getting into the specialized realms of programming. Personally, I would use LinqToXML as a first option if your platform (.net 3.5 or higher) will support it. Google it and see if it will work for you.