欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#中xml的序列化和反序列化

程序员文章站 2022-03-04 14:17:03
...

反序列化方法: 

        /// <summary>  
        /// 反序列化  
        /// </summary>  
        /// <param name="type">类型</param>  
        /// <param name="xml">XML字符串</param>  
        /// <returns></returns>  
        public static object Deserialize(Type type, string xml)
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }

 

序列化方法: 

        /// <summary>  
        /// 序列化  
        /// </summary>  
        /// <param name="type">类型</param>  
        /// <param name="obj">对象</param>  
        /// <returns></returns>  
        public static string Serializer(Type type, object obj)
        {
            var settings = new XmlWriterSettings
            {
                Indent = true,
                IndentChars = " ",
                NewLineChars = "\r\n",
                Encoding = Encoding.UTF8,
                OmitXmlDeclaration = true // 不生成声明头
            };
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(type);

            using (XmlWriter xmlWriter = XmlWriter.Create(Stream, settings))
            {

                XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                namespaces.Add(string.Empty, string.Empty);
                //序列化对象  
                xml.Serialize(xmlWriter, obj, namespaces);
                xmlWriter.Close();
            }
            Stream.Position = 0;
            StreamReader sr = new StreamReader(Stream);
            string str = sr.ReadToEnd();

            sr.Dispose();
            Stream.Dispose();

            return str;
        }

使用例子: 

        public string HelloWorld()
        {
            string xml = @"<MSG>
    <MSH>
        <ORG_CODE>1</ORG_CODE>
        <InCODE>1</InCODE>
        <BaseFun>1</BaseFun>
    </MSH>
    <ASK>
    <PAR>
            <CFYBM>1</CFYBM>
            <CJGMC>1</CJGMC>
            <CKSBM>1</CKSBM>
            <ITYPE>1</ITYPE>
            <GLZ>1</GLZ>
        </PAR>
    </ASK>
</MSG> ";
            try
            {
                //把xml反序列化成对象,主要你的对象里的属性名要和xml标签一样,不然会解析失败;
                //把Model.M01000.Input.MSG替换成你的对象即可;
                Model.M01000.Input.MSG msg = new Model.M01000.Input.MSG();
                msg = XmlSerializeUtil.Deserialize(typeof(Model.M01000.Input.MSG), xml) as Model.M01000.Input.MSG;
            }
            catch (Exception ex)
            {
                Pub.WriteError("==解析XML失败==" + xml + Environment.NewLine + ex.ToString());
            }

            //这里就是把对象给序列化成xml
            //也是把Model.M01000.Ouput.Normal.MSG替换成你的对象即可;
            Model.M01000.Ouput.Normal.MSG rnmsg = new Model.M01000.Ouput.Normal.MSG();
            string strmsg = XmlSerializeUtil.Serializer(typeof(Model.M01000.Ouput.Normal.MSG), rnmsg);

            return "Hello World";
        }

亲测可用,注意对象属性名要和xml标签一样,注意对象属性名要和xml标签一样,注意对象属性名要和xml标签一样!!!