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

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


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

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


[XmlRoot(ElementName = "PARM")]

public class ParmInfo

{

    [XmlElement("KEY")]

    public string ParmKey { get; set; }


    [XmlElement("VALUE")]

    public string ParmVal { get; set; }


    public ParmInfo()

    {


    }


    public ParmInfo(string inpParmKey, string inpParmVal)

    {

        ParmKey = inpParmKey;

        ParmVal = inpParmVal;

    }

}

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


谢谢!


慕神8447489
浏览 160回答 2
2回答

LEATH

如果您想要两个 XML 的单个类,请尝试此操作string xmlText1 = @"<PARM KEY=""K1"" VALUE=""V1""/>";string xmlText2 = @"<PARM><KEY>K2</KEY><VALUE>V2</VALUE></PARM>";[XmlRoot(ElementName = "PARM")]public class ParmInfo{&nbsp; &nbsp; [XmlAttribute("KEY")]&nbsp; &nbsp; public string ParmAttrKey { get; set; }&nbsp; &nbsp; [XmlAttribute("VALUE")]&nbsp; &nbsp; public string ParmAttrVal { get; set; }&nbsp; &nbsp; [XmlElement("KEY")]&nbsp; &nbsp; public string ParmEleKey { get; set; }&nbsp; &nbsp; [XmlElement("VALUE")]&nbsp; &nbsp; public string ParmEleVal { get; set; }&nbsp; &nbsp; public ParmInfo()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public ParmInfo(string inpParmEleKey, string inpParmEleVal, string inpParmAttrKey, string inpParmAttrVal)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ParmEleKey = inpParmEleKey;&nbsp; &nbsp; &nbsp; &nbsp; ParmEleVal = inpParmEleVal;&nbsp; &nbsp; &nbsp; &nbsp; ParmAttrKey = inpParmAttrKey;&nbsp; &nbsp; &nbsp; &nbsp; ParmAttrVal = inpParmAttrVal;&nbsp; &nbsp; }}这是我用来序列化的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);

慕哥6287543

这就是我最终完成的事情。我希望避免这种情况。但无论如何。[Serializable][XmlRoot(ElementName = "PARM")]public class ParmInfo{&nbsp; &nbsp; private string _ParmName;&nbsp; &nbsp; private string _ParmVal;&nbsp; &nbsp; private bool _UsingAttr = false;&nbsp; &nbsp; [XmlElement("KEY", IsNullable = true)]&nbsp; &nbsp; public string ParmName&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _ParmName;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!string.IsNullOrEmpty(value))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ParmName = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _UsingAttr = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [XmlElement("VALUE", IsNullable = true)]&nbsp; &nbsp; public string ParmVal&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _ParmVal;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!string.IsNullOrEmpty(value))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ParmVal = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [XmlAttribute("KEY")]&nbsp; &nbsp; public string ParmNameAttr&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _ParmName;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!string.IsNullOrEmpty(value))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ParmName = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _UsingAttr = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [XmlAttribute("VALUE")]&nbsp; &nbsp; public string ParmValueAttr&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _ParmVal;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!string.IsNullOrEmpty(value))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ParmVal = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public ParmInfo()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public ParmInfo(string inpParmName, string inpParmVal)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ParmName = inpParmName;&nbsp; &nbsp; &nbsp; &nbsp; _ParmVal = inpParmVal;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP