views:

75

answers:

2

I have to send information too a third party in an XML format they have specified, a very common task I'm sure.

I have set of XSD files and, using XSD.exe, I have created a set of types. To generate the XML I map the values from the types within my domain to the 3rd party types:

public ExternalBar Map(InternalFoo foo) {
    var bar = new ExternalBar;

    bar.GivenName = foo.FirstName;
    bar.FamilyName = foo.LastName;

    return bar;

}

I will then use the XMLSerializer to generate the files, probably checking them against the XSD before releasing them.

This method is very manual though and I wonder if there is a better way using the Framework or external tools to map the data and create the files.

A: 

Firstly, I'm assuming that the object properties in your existing domain map to the 3rd party types without much manipulation, except for the repetitive property assignments.

So I'd recommend just using standard XML serialization of your domain tree (generate an outbound schema for your classes using XSD), then post-processing the result via a set of XSLT stylesheets. Then after post-processing, validate the resulting XML documents against the 3rd party schemas.

It'll probably be more complicated than that, because it really depends on the complexity of the mapping between the object domains, but this is a method that I've used successfully in the past.

As far as GUI tools are concerned I've heard (but not used myself) that Stylus Studio is pretty good for schema-to-schema mappings (screenshot here).

devstuff
A: 

LINQ to XML works quite well for this... e.g.

XElement results = new XElement("ExternalFoos",
    from f in internalFoos
    select new XElement("ExternalFoo", new XAttribute[] {
        new XAttribute("GivenName", f.FirstName),
        new XAttribute("FamilyName", f.LastName) } ));
mancaus