首页 > 其他分享 >CSharp: XML Serialize and Deserialize

CSharp: XML Serialize and Deserialize

时间:2023-02-11 10:00:56浏览次数:60  
标签:XML set Deserialize get Serialize part new du public

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.Xml;


namespace DuFullCalendar
{



    [XmlRoot("ArrayOfString")]
    public class ArrayOfString
    {

        [XmlElement("CityName")]
        public string CityName {get;set;}

        [XmlElement("CityCode")]
        public string CityCode { get; set; }


    }

    /// <summary>
    /// 
    /// </summary>
    [XmlRoot]
    public class Teacher
    {
        [XmlAttribute]
        public string LessonTitle { get; set; }

        [XmlElement]
        public General general { get; set; }

        [XmlElement]
        public Descrption descrption { get; set; }

        [XmlElement("Part")]
        public List<Part> partlist { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class General
    {
        [XmlAttribute]
        public string title { get; set; }

        [XmlAttribute]
        public string msg { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class Descrption
    {
        [XmlAttribute]
        public string document { get; set; }
    }

    /// <summary>
    /// 
    /// </summary>
    public class Part
    {
        [XmlAttribute]
        public string PartTitle { get; set; }

        [XmlAttribute]
        public string PartNum { get; set; }

        [XmlAttribute]
        public string PartOver { get; set; }

       /// [XmlElement(ElementName = "Sco", Type = typeof(Sco), IsNullable = true)]
        //[XmlElement(ElementName = "Quest", Type = typeof(Quest), IsNullable = true)]

        public List<GeovinDu> GeovinDus { get; set; }
        [XmlElement(ElementName = "Sco")]
        public Sco sco { get; set; }
        [XmlElement(ElementName = "quest")]
        public Quest quest { get; set; }


    }
    /// <summary>
    /// 
    /// </summary>
    public class GeovinDu
    {
        [XmlElement("id")]
        public int Id { get; set; }
        [XmlElement("KeyWord")]
        public string KeyWord { get; set; }

    }

    /// <summary>
    /// 
    /// </summary>
    public class Sco : GeovinDu
    {
        [XmlAttribute]
        public string ScoTitle { get; set; }

        [XmlAttribute]
        public string isok { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class Quest : GeovinDu
    {
        [XmlAttribute]
        public string Title { get; set; }
    }

}

  

调用:

    //ArrayOfString arstring = new ArrayOfString() { 
            // CityCode="0001", CityName="北京"            
            //};
            //string file = Server.MapPath("geovindu.xml");
            //XmlSerializer xswrite = new XmlSerializer(typeof(ArrayOfString));
            //写文件
            //Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);
            //xswrite.Serialize(stream, arstring);

            //读文件
            //Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
            //ArrayOfString p = xswrite.Deserialize(stream) as ArrayOfString;
            //Response.Write(p.CityName);

            Teacher teacher = new Teacher();
            teacher.LessonTitle = "geovindu 涂聚文";

            General general = new General();
            general.title = "ms";
            general.msg = "hi,how are you";
            teacher.general = general;
            Descrption descrption = new Descrption();
            descrption.document = "你好,吗";
            teacher.descrption = descrption;
            List<Part> partlist = new List<Part>();
            Part part = new Part();
            part.PartNum = "1";
            part.PartOver = "2";
            part.PartTitle = "p";

            Sco sco = new Sco();
            sco.ScoTitle = "sco";
            sco.isok = "ok";
            part.sco= sco;
            Quest quest = new Quest();
            quest.Title = "quest";
            part.quest = quest;
            List<GeovinDu> geovindu = new List<GeovinDu>();
            GeovinDu du = new GeovinDu();
            du.Id = 1;
            du.KeyWord = "geovindu";
            geovindu.Add(du);
            du = new GeovinDu();
            du.Id = 2;
            du.KeyWord = "geovindu2";
            geovindu.Add(du);
            part.GeovinDus = geovindu;
            partlist.Add(part);

            part = new Part();
            part.PartNum = "11";
            part.PartOver = "22";
            part.PartTitle = "pp";

             sco = new Sco();
            sco.ScoTitle = "sco";
            sco.isok = "ok";
            part.sco = sco;

             quest = new Quest();
            quest.Title = "quest";

            part.quest = quest;

           geovindu = new List<GeovinDu>();
             du = new GeovinDu();
            du.Id = 2;
            du.KeyWord = "geovindu";
            geovindu.Add(du);
            du = new GeovinDu();
            du.Id = 2;
            du.KeyWord = "geovindu2";
            geovindu.Add(du);
            part.GeovinDus = geovindu;
            partlist.Add(part);

            teacher.partlist = partlist;
            
            string file = Server.MapPath("geovinduteach.xml");
            XmlSerializer xswrite = new XmlSerializer(typeof(Teacher));
            //Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);
            //xswrite.Serialize(stream, teacher);

            Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
            Teacher p = xswrite.Deserialize(stream) as Teacher;
            Response.Write(p.LessonTitle);

  

xml file

<?xml version="1.0"?>
<Teacher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" LessonTitle="geovindu 涂聚文">
  <general title="ms" msg="hi,how are you" />
  <descrption document="你好,吗" />
  <Part PartTitle="p" PartNum="1" PartOver="2">
    <GeovinDus>
      <GeovinDu>
        <id>1</id>
        <KeyWord>geovindu</KeyWord>
      </GeovinDu>
      <GeovinDu>
        <id>2</id>
        <KeyWord>geovindu2</KeyWord>
      </GeovinDu>
    </GeovinDus>
    <Sco ScoTitle="sco" isok="ok">
      <id>0</id>
    </Sco>
    <quest Title="quest">
      <id>0</id>
    </quest>
  </Part>
  <Part PartTitle="pp" PartNum="11" PartOver="22">
    <GeovinDus>
      <GeovinDu>
        <id>2</id>
        <KeyWord>geovindu</KeyWord>
      </GeovinDu>
      <GeovinDu>
        <id>2</id>
        <KeyWord>geovindu2</KeyWord>
      </GeovinDu>
    </GeovinDus>
    <Sco ScoTitle="sco" isok="ok">
      <id>0</id>
    </Sco>
    <quest Title="quest">
      <id>0</id>
    </quest>
  </Part>
</Teacher>

  

标签:XML,set,Deserialize,get,Serialize,part,new,du,public
From: https://www.cnblogs.com/geovindu/p/17110925.html

相关文章

  • C#使用openxml-sdk生成word文档
    从nuget中安装openxml  测试代码using(WordprocessingDocumentdoc=WordprocessingDocument.Create("test.docx",DocumentFormat.OpenXml.Wordproc......
  • pom.xml
    <!--文件拷贝时的编码jdk版本--><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outp......
  • 使用配置类替代xml配置文件
    创建配置类packagecom.xxx.spring.config;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configur......
  • 基于xml的引入外部属性文件
    例:配置druid数据源新建属性文件jdbc.propertiesdruid.driverClassName=com.mysql.jdbc.Driverdruid.url=jdbc:mysql://localhost:3306/testdruid.username=rootdruid......
  • web.xml文件简述
    tomcat的conf中:是全局配置,配置公共信息。Web项目中:是局部配置,只针对本项目有效。加载顺序:ServletContext->context-param->listener->filter->se......
  • pom.xml 阿里mvn云仓库
    <repositories><repository><id>aliyun</id><url>https://maven.aliyun.com/repository/public</url><releases><enabled>t......
  • QT Dom修改xml
    QFilefile("Init.xml");if(!file.open(QFile::ReadOnly))return;QDomDocumentdoc;if(!doc.setContent(&file)){file.close();re......
  • Java web.xml之contextConfigLocation作用案例详解
    在WEB.xml中通过contextConfigLocation配置spring,contextConfigLocation参数定义了要装入的Spring配置文件。部署applicationContext.xml文件        如果不......
  • 297. Serialize and Deserialize Binary Tree[Hard]
    297.SerializeandDeserializeBinaryTreeSerializationistheprocessofconvertingadatastructureorobjectintoasequenceofbitssothatitcanbestor......
  • 高效XML绑定框架JIBX
    高效XML绑定框架JIBXdemo源码地址https://gitee.com/clover-clover/clover.git具体路径:clover/clover-frame/clover-frame-netty/src/main/java/com/clover/frame/nett......