Xml字符串反序列化为实体问题

public class XmlDataSerializer
{
    public object GetObjectData(string stream,Type type)
    {
        if (string.IsNullOrEmpty(stream))
            return null;

        stream = stream.Replace("<MobileNote>", "<" + type.Name + ">");
        stream = stream.Replace("</MobileNote>", "</" + type.Name + ">");

        stream = stream.Replace("<object />", "<theInquired />");
        stream = stream.Replace("<object>", "<theInquired>");
        stream = stream.Replace("</object>", "</theInquired>");

        XmlReader xmlReader = XmlReader.Create(new StringReader(stream));
        XmlSerializer mySerializer = new XmlSerializer(type);
        return mySerializer.Deserialize(xmlReader);
    }

stream为Xml字符串数据。type为tpyeof(类名)。

    //public Questionnaire GetQuestionnaireData(string stream)
    //{
    //    if (string.IsNullOrEmpty(stream))
    //        return null;

    //    stream = stream.Replace("<MobileNote>", "<Questionnaire>");
    //    stream = stream.Replace("</MobileNote>", "</Questionnaire>");

    //    stream = stream.Replace("<object />", "<theInquired />");
    //    stream = stream.Replace("<object>", "<theInquired>");
    //    stream = stream.Replace("</object>", "</theInquired>");

    //    XmlReader xmlReader = XmlReader.Create(new StringReader(stream));
    //    XmlSerializer mySerializer = new XmlSerializer(typeof(Questionnaire));
    //    return mySerializer.Deserialize(xmlReader) as Questionnaire;
    //}

扫描二维码关注公众号,回复: 6740686 查看本文章

    //public SiteRecordsOfInvestigation GetSiteRecordsOfInvestigationData(string stream)
    //{
    //    if (string.IsNullOrEmpty(stream))
    //        return null;

    //    stream = stream.Replace("<MobileNote>", "<SiteRecordsOfInvestigation>");
    //    stream = stream.Replace("</MobileNote>", "</SiteRecordsOfInvestigation>");

    //    XmlReader xmlReader = XmlReader.Create(new StringReader(stream));
    //    XmlSerializer mySerializer = new XmlSerializer(typeof(SiteRecordsOfInvestigation));
    //    return mySerializer.Deserialize(xmlReader) as SiteRecordsOfInvestigation;
    //}
}

呵呵。。。把上面连个方法改为一个通用的方法,不知道算不算重构。。。

转载于:https://www.cnblogs.com/lanchong/archive/2012/03/06/2382292.html

猜你喜欢

转载自blog.csdn.net/weixin_34315189/article/details/94705017