views:

44

answers:

2

I need to know How I can achieve this goal by classes :

we have two different applications in the company (App1 , App2)

Appl can export xml with know items ( ID , Name)

we need app2 to import this data but App2 display different items (CarID, CarName) and this items defined like this with the mapping info

<CarID>
<Mapping name="ID"/>
</CarID>
<CarNAme>
 <Mapping name="Name"/>
</CarNAme>"

How I can achieve this as classes or ARCHITECTURE , i will develop this with c# I need one interface because we may support different type of files not just xml

+1  A: 

You can use attributes to map properties to xmlelements.
The classes Car1 and Car2 will generate the same xml when serialized.

[Serializable]
[XmlRoot("Car")]
public class Car1
{
    [XmlElement("CarId")]
    public int Id { get; set; }
    [XmlElement("CarName")]
    public string Name { get; set; }
}
[Serializable]
[XmlRoot("Car")]
public class Car2
{
    public int CarId { get; set; }
    public string CarName { get; set; }
}

[TestFixture]
public class CarTest
{
    [Test]
    public void SerializationTest()
    {
        var ms = new MemoryStream();
        var car1 = new Car1 {Id = 10, Name = "Car1"};
        var xs = new XmlSerializer(typeof(Car1));
        var tw = new StreamWriter(ms);
        xs.Serialize(tw, car1);

        ms.Seek(0, SeekOrigin.Begin);
        xs = new XmlSerializer(typeof(Car2));
        var tr = new StreamReader(ms);
        var car2 = (Car2)xs.Deserialize(tr);

        Assert.AreEqual(car1.Id, car2.CarId);
        Assert.AreEqual(car1.Name, car2.CarName);
    }
}
Jens Granlund
A: 

Won't your problem be solved if you introduce 2 type of files? A configuration file which defines the actual mapping and separate data files that have the same format.

kalkie