zoukankan      html  css  js  c++  java
  • 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;
        //}

        //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;
        //}
    }

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

    学习的路上,分享的知识有不当的地方,希望大家指出。 感谢大家的阅读,希望这些分享能够给您带来帮助。
  • 相关阅读:
    hibernate对应关系详解(转)
    mybatis genertor两种使用方式(文件+项目)
    YII2 union 不同数据结构时 解决方案
    Yii2 分表后 使用 union all 分页实现代码
    Beyond Compare 4.2.10手动破解
    Xshell 6+Xftp 6官方下载免费版
    Navicat Premium
    yii2的Console定时任务创建
    内嵌多个youtube视频并展现该频道所有视频列表
    video.js 动态获取URL 并播放youtube视频
  • 原文地址:https://www.cnblogs.com/lanchong/p/2382292.html
Copyright © 2011-2022 走看看