views:

48

answers:

3

Hi,

I have two xml files with the same data but different tags. I need to serialise them into an object. At first i tried to create the classes:

[XmlRoot(ElementName="ONIXMessage")]
public class ONIXMessage
{
    [XmlAttribute(AttributeName="release")]
    public string Release { get; set; }

    [XmlElement("Header")]
    public Header Header { get; set; }

    [XmlElement("Product")]
    public List<Product> Products { get; set; }        
}

However i'd need to create another class for the xml with different tags. Unless of course i find a better way to deserialise them. I currently have something like this:

XmlSerializer serializer = new
        XmlSerializer(type);

        FileStream fs = new FileStream(filename, FileMode.Open);
        XmlReader reader = new XmlTextReader(fs);

        return (ONIXMessage)serializer.Deserialize(reader);

Hope i'm making sense.

A: 

How about XmlChoiceIdentifier?

Scoregraphic
A: 

At the very least, write your code properly:

XmlSerializer serializer = new XmlSerializer(type); 

using (FileStream fs = new FileStream(filename, FileMode.Open))
{
    using (XmlReader reader = XmlReader.Create(fs))
    {
        return (ONIXMessage)serializer.Deserialize(reader);
    }
}

// Marked as CW, since this is really just a formatted comment.

John Saunders
+1  A: 

Look at OXM which is xml mapping framework. With OXM you can create 2 mapping classes that map to the same object. I think this is a neat solution to your situation.

Delucia
brilliant, this is exactly what i was looking for.
Simon