I have an XML template file and a previously saved XML database record:
- XML file – contains all the current fields I want for an object – This may change over time with items added
- XML database record – this is the saved XML. It may be different from the XML in point one because the “template” xml may have been updated to contain more fields, etc.
An example will clear this up..
I have a “template” xml that looks like this:
<dvd displayText=""> <field id="txtTitle" displayText="true" required="true" type="textbox" width="100" label="DVD Title" /> <field id="txtDescription" displayText="true" required="true" type="textbox" width="50" label="Your Description" /> </dvd>
This tells the UI to collect a Title and Description when creating a new record. This works perfectly.
So when I create a new DVD record it will display these 2 fields to the user and collect the “Title” and “Description” values and save the value into an XML field in a SQL Server table.
But what I want to do now is add a new “field” definition to the template xml. I’ve done this.. for the purposes of this example, lets have a “PurchasedDate” field definition added to the template XML.
<dvd displayText=""> <field id="txtTitle" displayText="true" required="true" type="textbox" width="100" label="DVD Title" /> <field id="txtDescription" displayText="true" required="true" type="textbox" width="50" label="Your Description" /> <field id="txtPurchaseDate" displayText="true" required="true" type="textbox" width="50" label="Purchase Date" /> </dvd>
If I’m creating a new DVD record, I get prompted for the 3 values; Title, Description, Purchased Date, because that’s whats in the template XML.. But if I’m recalling a previously saved DVD record, it doesn’t have the new “Purchased Date” field in the XML..
<dvd displayText="My DVD"> <field id="txtTitle" displayText="true" required="true" type="textbox" width="100" label="DVD Title">My Movie DVD</field> <field id="txtDescription" displayText="true" required="true" type="textbox" width="50" label="Your Description">This movie is awesome</field> </dvd>
So what I want to do is take the XML saved in the database (shown above), compare or merge any missing “field” definitions from the template XML file.. and end up with this below:
<dvd displayText="My DVD"> <field id="txtTitle" displayText="true" required="true" type="textbox" width="100" label="DVD Title">My Movie DVD</field> <field id="txtDescription" displayText="true" required="true" type="textbox" width="50" label="Your Description">This movie is awesome</field> <field id="txtPurchaseDate" displayText="true" required="true" type="textbox" width="50" label="Purchase Date" /> </dvd>
I’m using Linq to get the value from the existing record from the database if one exists.
and I’m currently doing a XmlDocument.LoadXml(xmlstring) or XmlDocument.Load(filename) depending on whether its new or existing record.
I’d then like to take the XmlDocument and merge the template into this to produce the above XML output for displaying on screen.
If there is a better way than using XmlDocuments, can you please let me know.. I’ll happily go and research it and then use it..
I’m using C#…
Many thanks..
Your solution has a huge redundancy problem, you will face similar problems if you for instance alters the display text for one of the fields, or change the required field.
I would separate the metadata from the actual data, i.e. keep one record that describes the fields:
And strip the data records down to field ids and values:
Use the meta xml to figure out what fields are possible to load and get the values from the stripped down xml.
Otherwise you will need to sync all data every time your record layout changes.