C# 序列化解析XML

/*********************XML文件*********************/

<?xml version="1.0" encoding="utf-8" ?>
<Interfaces>
  <!--获取某个成员所属的组织机构列表-->
   <Methods>
    <ServerName>
      dnzl.org.list.get
    </ServerName>
    <Content>
      {}
    </Content>
  </Methods>

</Interfaces>

/*********************XML文件*********************/

/*********************Model*********************/

    public class Method
    {
        [XmlElement("ServerName")]
        public string ServerName { get; set; }
        [XmlElement("Content")]
        public string Content { get; set; }
    }
    public class Interfaces
    {
        [XmlElement("Methods")]
        public List<Method> Methods { get; set; }
    }

/*********************Model*********************/

/*********************C#代码*********************/       

XmlDocument xmldoc = new XmlDocument();


        //获取物理路径
        string path = Server.MapPath("~/测试结果/TestData2.xml");
        //加载xml文档...
        xmldoc.Load(path);

        MemoryStream ms = new MemoryStream();
        xmldoc.Save(ms);
        ms.Position = 0;

        //声明序列化对象实例serializers
        XmlSerializer serializer = new XmlSerializer(typeof(Interfaces));
        //反序列化,并将反序列化结果值赋给变量i
        Interfaces interfaceList = (Interfaces)serializer.Deserialize(ms);

/*********************C#代码*********************/  

posted on 2013-02-01 14:52  不会写代码的小斌  阅读(243)  评论(0)    收藏  举报

导航