Reputation: 846
I have two xml documents with about 1000 elements in each of them and they both have different schemas. I am trying to map document A to document B and wanted to know what the best method to do this would be.
Currently I am grabbing all the elements from document A then creating a series of XElements to be placed into document B. I am using the values from document A's XElements to populate the values of document B's XElements. I then place these newly created XElements into a basic xml document. This document is a stripped down version of document B which only contains the basic elements—kind of like an outer shell. Here is some sample code:
XElement docAName = docA.Descendants(DocANameSpace + "Name").FirstOrDefault();
XElement docBName = new XElement("{http://namespace.com}Name", docAName.Value);
docBTemplate.Descendants(DocBNameSpace + "Names").FirstOrDefault().Add(DocBName);
However, there is so many elements that I wound up with a method of over 1000 lines and it just looks sloppy. Anyone have any suggestions on a better way to map these documents? I don't know of an XSLT Transformation will work since the schemas are so different.
Edit:I looked into XSL Transform and it sounds like it would be quite a bit of work. The two xml documents are very different and it would require massive restructuring. This isn't a simple one-to-one replace one elements value with another. I am changing the names, namespaces, locations, hierarchical structure and even some of the values, which have to be slightly altered in order to conform to document B's schema. It sounds like XSL Transform would be better used when mapping two small documents where you would just need to swap some values and element names. Amy I correct in thinking this?
Upvotes: 2
Views: 2505
Reputation: 60206
If I understand you correctly you want to change a namespace to a different one and strip a few elements?
I would use an XSLT transform - IMHO the tool for XML-to-XML transformation. How different the schemas are doesn't matter really, why should it? Making partial copies and applying changes to element namespace is a very simple task in XSLT.
Upvotes: 2