C # xml format string deserialize and serialize

Recently doing C # a project that involves a sequence of knowledge, but for complex xml format does not know how to write entity classes online did not find the blog data xml same type of format of presentation, the study for two days, finally did come out , the first string in XML format Tieshanglai.

<REQUEST_COMMON_FPKJ class="REQUEST_COMMON_FPKJ"> 
        <COMMON_FPKJ_FPT class="COMMON_FPKJ_FPT"> 
            <FPQQLSH>CW201906271117030642</FPQQLSH> 
            <DDH></DDH> 
            <KPLX>0</KPLX>
            <BMB_BBH>30.0</BMB_BBH> 
            <XSF_NSRSBH>15000119230104170217</XSF_NSRSBH> 
            <XSF_MC>15000119230104170217</XSF_MC> 
            <XSF_DZDH>中国杭州西湖区龙井路002号</XSF_DZDH> 
            <XSF_YHZH></XSF_YHZH> 
            <GMF_NSRSBH></GMF_NSRSBH> 
            <GMF_MC>Gwyy001</GMF_MC> 
            <GMF_DZDH></GMF_DZDH> 
            <GMF_YHZH></GMF_YHZH>
        <GMF_SJ></GMF_SJ>
        <GMF_EMAIL></GMF_EMAIL> 
            <KPR>Kwyy001</KPR> 
            <SKR></SKR> 
            <FHR></FHR> 
            <YFP_DM></YFP_DM> 
            <YFP_HM></YFP_HM> 
            <JSHJ>99.9</JSHJ> 
            <HJJE>90</HJJE> 
            <HJSE>9.9</HJSE> 
            <BZ></BZ> 
           <ZXBMmm></ZXBMmm>
        </COMMON_FPKJ_FPT> 
        <COMMON_FPKJ_XMXXS class="COMMON_FPKJ_XMXX" size="2"> 
            <COMMON_FPKJ_XMXX> 
                <FPHXZ>2</FPHXZ> 
                <SPBM>103011102</SPBM>
                <ZXBM></ZXBM>
                <YHZCBS>0</YHZCBS>
                <LSLBS></LSLBS>
                <ZZSTSGL></ZZSTSGL>
                <XMMC>水果</XMMC> 
                <GGXH></GGXH> 
                <DW>kk</DW> 
                <XMSL>1</XMSL> 
                <XMDJ>100</XMDJ> 
                <XMJE>100</XMJE> 
                <SL>0.11</SL> 
                <SE>11</SE> 
            </COMMON_FPKJ_XMXX>
             <COMMON_FPKJ_XMXX> 
                <FPHXZ>1</FPHXZ> 
                <SPBM>103011102</SPBM>
        <ZXBM></ZXBM>
                <YHZCBS>0</YHZCBS>
                <LSLBS></ LSLBS>                 <XMMC> Fruit </ XMMC> 
                <ZZSTSGL> </ ZZSTSGL>

                <GGXH></GGXH> 
                <DW></DW> 
                <XMSL></XMSL> 
                <XMDJ></XMDJ> 
                <XMJE>-10</XMJE> 
                <SL>0.11</SL> 
                <SE>-1.1</SE> 
            </COMMON_FPKJ_XMXX>
        </COMMON_FPKJ_XMXXS> 
     </REQUEST_COMMON_FPKJ>

 

Entity Class Code:

namespace CW.HF.HIS.Test
{
    [XmlRoot("REQUEST_COMMON_FPKJ")]
    public class REQUEST_COMMON
    {
        [XmlAttribute("class")]
        public string _class
        { get; set; } = "REQUEST_COMMON_FPKJ";

        private COMMON_FPKJ_FPT commonFpkjFpt = null;
        //属性的定义
        [XmlElement("COMMON_FPKJ_FPT")]
        public COMMON_FPKJ_FPT COMMON_FPKJ_FPT
        {
            get
            {
                if (commonFpkjFpt == null)
                {
                    commonFpkjFpt = new COMMON_FPKJ_FPT();
                }
                return commonFpkjFpt;
            }
            set { commonFpkjFpt = value; }
        }

        private COMMON_FPKJ_XMXXS commonFpkjXmxxs = null;
        [XmlElement("COMMON_FPKJ_XMXXS")]
        public COMMON_FPKJ_XMXXS COMMON_FPKJ_XMXXS
        {
            get
            {
                if (commonFpkjXmxxs == null)
                {
                    commonFpkjXmxxs = new COMMON_FPKJ_XMXXS();
                }
                return commonFpkjXmxxs;
            }
            set { commonFpkjXmxxs = value; }
        }
    }

    public class COMMON_FPKJ_FPT
    {
        [XmlAttribute("class")]
        public string _class
        { get; set; } = "COMMON_FPKJ_FPT";

        [XmlElement("FPQQLSH")]
        public string FPQQLSH { get; set; } = string.Empty;

        [XmlElement("DDH")]
        public string DDH
        { get; set; } = string.Empty;

        [XmlElement("KPLX")]
        public string KPLX
        { get; set; } = string.Empty;

        [XmlElement("BMB_BBH")]
        public string BMB_BBH { get; set; } = string.Empty;

        [XmlElement("XSF_NSRSBH")]
        public string XSF_NSRSBH
        { get; set; } = string.Empty;

        [XmlElement("XSF_MC")]
        public string XSF_MC
        { get; set; } = string.Empty;

        [XmlElement("XSF_DZDH")]
        public string XSF_DZDH { get; set; } = string.Empty;

        [XmlElement("XSF_YHZH")]
        public string XSF_YHZH
        { get; set; } = string.Empty;

        [XmlElement("GMF_NSRSBH")]
        public string GMF_NSRSBH
        { get; set; } = string.Empty;

        [XmlElement("GMF_MC")]
        public string GMF_MC { get; set; } = string.Empty;

        [XmlElement("GMF_DZDH")]
        public string GMF_DZDH
        { get; set; } = string.Empty;

        [XmlElement("GMF_YHZH")]
        public string GMF_YHZH
        { get; set; } = string.Empty;

        [XmlElement("GMF_SJ")]
        public string GMF_SJ
        { get; set; } = string.Empty;

        [XmlElement("GMF_EMAIL")]
        public string GMF_EMAIL
        { get; set; } = string.Empty;

        [XmlElement("KPR")]
        public string KPR
        { get; set; } = string.Empty;

        [XmlElement("SKR")]
        public string SKR
        { get; set; } = string.Empty;


        [XmlElement("FHR")]
        public string FHR
        { get; set; } = string.Empty;

        [XmlElement("YFP_DM")]
        public string YFP_DM
        { get; set; } = string.Empty;

        [XmlElement("YFP_HM")]
        public string YFP_HM
        { get; set; } = string.Empty;

        [XmlElement("JSHJ")]
        public string JSHJ
        { get; set; } = string.Empty;

        [XmlElement("HJJE")]
        public string HJJE
        { get; set; } = string.Empty;

        [XmlElement("HJSE")]
        public string HJSE
        { get; set; } = string.Empty;

        [XmlElement("BZ")]
        public string BZ
        { get; set; } = string.Empty;
    }

    public class COMMON_FPKJ_XMXXS
    {
        [XmlAttribute("class")]
        public string _class { get; set; } = "COMMON_FPKJ_XMXX";

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

        private List<COMMON_FPKJ_XMXX> commonFpkjXmxxs = null;

        [XmlElement("COMMON_FPKJ_XMXX")]
        public List<COMMON_FPKJ_XMXX> FPKJ_XMXXs
        {
            get
            {
                if (commonFpkjXmxxs == null)
                {
                    commonFpkjXmxxs = new List<COMMON_FPKJ_XMXX>();
                }
                return commonFpkjXmxxs;
            }
            set { commonFpkjXmxxs = value; }
        }
    }


    public class COMMON_FPKJ_XMXX
    {
        [XmlElement("FPHXZ")]
        public string FPHXZ { get; set; } = string.Empty;

        [XmlElement("SPBM")]
        public string SPBM { get; set; } = string.Empty;

        [XmlElement("ZXBM")]
        public string ZXBM { get; set; } = string.Empty;

        [XmlElement("YHZCBS")]
        public string YHZCBS { get; set; } = string.Empty;
        [XmlElement("LSLBS")]
        public string LSLBS { get; set; } = string.Empty;

        [XmlElement("ZZSTSGL")]
        public string ZZSTSGL { get; set; } = string.Empty;
        [XmlElement("XMMC")]
        public string XMMC { get; set; } = string.Empty;

        [XmlElement("GGXH")]
        public string GGXH { get; set; } = string.Empty;
        [XmlElement("DW")]
        public string DW { get; set; } = string.Empty;

        [XmlElement("XMSL")]
        public string XMSL { get; set; } = string.Empty;

        [XmlElement("XMDJ")]
        public string XMDJ { get; set; } = string.Empty;

        [XmlElement("XMJE")]
        public string XMJE { get; set; } = string.Empty;

        [XmlElement("SL")]
        public string SL { get; set; } = string.Empty;

        [XmlElement("SE")]
        public string SE { get; set; } = string.Empty;
    }
}

And serialization code online more deserialization, not one pasted

The following is a code sequence, results do not include the file header xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/ XMLSchema "

/// <summary>
        /// 序列化
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string SerializeXml(object data)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xz = new XmlSerializer(data.GetType());
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                xz.Serialize(sw, data, ns);
                return sw.ToString();
            }
        }

Here is another code sequence, the file header contains the result xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/ 2001 / XMLSchema "

public static string Serializer(Type type, object obj)
        {
            MemoryStream Stream = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(type);
            //序列化对象
            xml.Serialize(Stream, obj);
            Stream.Position = 0;
            StreamReader sr = new StreamReader(Stream);
            string str = sr.ReadToEnd();

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

            return str;
        }

Guess you like

Origin blog.csdn.net/feitiankoulan/article/details/95306058