Search code examples
c#xsdlinq-to-xmllinq-to-xsd

Handle XML Schema changes without code changes


Currently we have a XML Schema and the code reads the xml file, validates against the schema and save to database. In future there would be schema changes, how can the code handles them without needing to rewrite the code for new schema.

Thanks,

Let me give an example

<Products>
   <product id="1">
      <name> ABC </name>
      <desc> good one </desc>
   </product>
</products>

XPath mapping configuration

Table      Column     XPath
Product    id         //Products/product/id
Product    name       //Products/product/name
Product    desc       //Products/product/desc

Now the C# code reads id, name and desc and generates an insert statement based on the Mapping configuraiton

If the schema changes and new element is added say price and we would add that price to mapping, so the new insert statement that is generated includes price.

Will this work?


Solution

  • I hate parsing XML and loading it into objects. Due to this, you can try the following approach.

    Create a C# object that represents the XML data you are talking about. Serialize that C# class, and viola you have an XML schema that is strongly typed. Also, if in the future you need additional schema changes, simply modify the C# class and reserialize and you're all set.

    This also removes the need to parse the XML document (assuming you are utilizing it within the CLR), as you can simply reference the C# class and you can deserialize it back into memory without any parsing.