将2种类型的XML反序列化为同一类

时间:2018-09-10 05:21:20

标签: c# .net xml

我有以下2种XML格式。是否可以有一个我可以反序列化这两种XML数据的类?如果是这样,我的类定义应该如何?

q

以上类将与xmlText2一起使用。我应该如何修改它以同时处理xmlText1和xmlText2?

谢谢!

2 个答案:

答案 0 :(得分:0)

如果您要两个XML都使用单个类,请尝试此操作

string xmlText1 = @"<PARM KEY=""K1"" VALUE=""V1""/>";
string xmlText2 = @"<PARM><KEY>K2</KEY><VALUE>V2</VALUE></PARM>";

[XmlRoot(ElementName = "PARM")]
public class ParmInfo
{
    [XmlAttribute("KEY")]
    public string ParmAttrKey { get; set; }

    [XmlAttribute("VALUE")]
    public string ParmAttrVal { get; set; }

    [XmlElement("KEY")]
    public string ParmEleKey { get; set; }

    [XmlElement("VALUE")]
    public string ParmEleVal { get; set; }

    public ParmInfo()
    {

    }

    public ParmInfo(string inpParmEleKey, string inpParmEleVal, string inpParmAttrKey, string inpParmAttrVal)
    {
        ParmEleKey = inpParmEleKey;
        ParmEleVal = inpParmEleVal;
        ParmAttrKey = inpParmAttrKey;
        ParmAttrVal = inpParmAttrVal;
    }
}

这是我用来序列化的东西

string xmlText1 = @"<PARM KEY=""K1"" VALUE=""V1""/>";
string xmlText2 = @"<PARM><KEY>K2</KEY><VALUE>V2</VALUE></PARM>";

XmlSerializer serializer = new XmlSerializer(typeof(ParmInfo));

StringReader stringReader = new StringReader(xmlText1);
var xmlText1Serialized = (ParmInfo)serializer.Deserialize(stringReader);

stringReader = new StringReader(xmlText2);
var xmlText2Serialized = (ParmInfo)serializer.Deserialize(stringReader);

答案 1 :(得分:0)

这就是我最终要做的。我希望避免这种情况。但是,无论如何。

[Serializable]
[XmlRoot(ElementName = "PARM")]
public class ParmInfo
{
    private string _ParmName;
    private string _ParmVal;
    private bool _UsingAttr = false;

    [XmlElement("KEY", IsNullable = true)]
    public string ParmName
    {
        get
        {
            return _ParmName;
        }
        set
        {
            if(!string.IsNullOrEmpty(value))
            {
                _ParmName = value;
                _UsingAttr = false;
            }
        }
    }

    [XmlElement("VALUE", IsNullable = true)]
    public string ParmVal
    {
        get
        {
            return _ParmVal;
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                _ParmVal = value;
            }
        }
    }

    [XmlAttribute("KEY")]
    public string ParmNameAttr
    {
        get
        {
            return _ParmName;
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                _ParmName = value;
                _UsingAttr = true;
            }
        }
    }

    [XmlAttribute("VALUE")]
    public string ParmValueAttr
    {
        get
        {
            return _ParmVal;
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                _ParmVal = value;
            }
        }
    }


    public ParmInfo()
    {

    }

    public ParmInfo(string inpParmName, string inpParmVal)
    {
        ParmName = inpParmName;
        _ParmVal = inpParmVal;
    }
}