File association architecture
I need to know how I can achieve this goal across classes:
we have two different apps in the company (App1, App2)
Application can export xml with know items (ID, Name)
we need app2 to import this data, but App2 displays different elements (CarID, CarName) and these elements defined in this way with mapping information
<CarID>
<Mapping name="ID"/>
</CarID>
<CarNAme>
<Mapping name="Name"/>
</CarNAme>"
How can I achieve this as classes or ARCHITECTURE, I will develop this with C # I need one interface because we can support different file types not only xml
+2
a source to share
2 answers
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);
}
}
+1
a source to share