我可以使用接口或多态性将两个不同的xml文件反序列化为一个对象类型吗?

时间:2022-01-27 20:12:51

I have two xml files which have the same data but different names for the tags. One has named reference tags while the other has a short version.

我有两个xml文件,它们的数据相同,但标记的名称不同。一个为引用标记命名,另一个为短版本。

As the data is exactly the same i want to be able to deserialise these files into objects, but I don't want to have to deal with two seperate objects.

由于数据完全相同,我希望能够将这些文件反序列化为对象,但我不希望必须处理两个分离的对象。

To deserialise the files i have created the following class:

为了对文件进行反序列化,我创建了以下类:

[XmlRoot(ElementName="Message")]
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; }        
}

For the files with the short tags i have this:

对于带有短标签的文件,我有:

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

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

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

I had a go using interfaces but wasn't really sure how to get the Header and Product to return correctly. They are different for each version too.

我尝试过使用接口,但不确定如何正确返回标题和产品。对于每个版本,它们都是不同的。

Is there a better way to deserialise the xml? Or can this still be done with what i already have?

有没有更好的方法去反序列化xml?或者这还能用我已经有的东西做吗?

Any help would be much appreciated.

非常感谢您的帮助。

Edit:

编辑:

Only took five years but I figured it out! This can be achieved by using XmlAttributeOverrides. http://www.ikriv.com/dev/dotnet/OverrideXml.shtml

只花了五年时间,但我发现了!这可以通过使用XmlAttributeOverrides来实现。http://www.ikriv.com/dev/dotnet/OverrideXml.shtml

1 个解决方案

#1


0  

Interfaces are probably the best way to go.. What I would do is mix up the interface with a factory, and do something similar to:

接口可能是最好的方法。我要做的就是把界面和工厂混在一起,做一些类似的事情:

public interface IMessageReader()
{
    ONIXMessage Read(string xml);
}

public static class ONIXMessageFactory
{
    private static IList<IMessageReader> readers = new List<IMessageReader>();

    public static ONIXMessage CreateMessage(string xml)
    {
      var reader = GetReader(xml);
      return reader.Read(xml);
    }

    public static IMessageReader GetReader(string xml)
    {
      // Somehow identify which reader would be required.
    }
}

Whereby you create two specialised readers and use a factory to control the creation of the message using whichever reader is required. The trickier part is identifying which reader would be the one to use.

您可以创建两个专用阅读器,并使用工厂来控制消息的创建。更棘手的部分是确定使用哪个阅读器。

Hope that gets you started...

希望这能让你开始……

#1


0  

Interfaces are probably the best way to go.. What I would do is mix up the interface with a factory, and do something similar to:

接口可能是最好的方法。我要做的就是把界面和工厂混在一起,做一些类似的事情:

public interface IMessageReader()
{
    ONIXMessage Read(string xml);
}

public static class ONIXMessageFactory
{
    private static IList<IMessageReader> readers = new List<IMessageReader>();

    public static ONIXMessage CreateMessage(string xml)
    {
      var reader = GetReader(xml);
      return reader.Read(xml);
    }

    public static IMessageReader GetReader(string xml)
    {
      // Somehow identify which reader would be required.
    }
}

Whereby you create two specialised readers and use a factory to control the creation of the message using whichever reader is required. The trickier part is identifying which reader would be the one to use.

您可以创建两个专用阅读器,并使用工厂来控制消息的创建。更棘手的部分是确定使用哪个阅读器。

Hope that gets you started...

希望这能让你开始……