现在的位置: 首页 > 综合 > 正文

.net中xml序列化的简单小实例

2018年05月18日 ⁄ 综合 ⁄ 共 2367字 ⁄ 字号 评论关闭

由于之前比较懒惰,懒得写博客,也懒得写总结,以至于开发经验已有一年多些的程序猿一枚仍旧处于入门阶段,所以决定做些改变,多写写技术博客,感觉这个挺好,既能锻炼文采,也能巩固技术,可谓一举多得。PS:目前对我这种入门级的小虾米来说,写几个技术博客主要是防止自己忘记做过哪些东西,学过哪些技术,其他的倒没什么大作用,好了,开始记录今天晚上的一个xml读取的小例子。

Control_Widget.xml(自己简单的设计了一下格式,基本上符合我的要求,简单易读,易于扩展)

<?xml version="1.0"?>
<TestProject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ControlList>
  <control_Widget>
    <Name>testName1</Name>
    <Value>testValue1</Value>
    <Property>testProperty1</Property>
    <Type>testType1</Type>
    <Validate>testValidate1</Validate>
  </control_Widget>
  <control_Widget>
    <Name>testName2</Name>
    <Value>testValue2</Value>
    <Property>testProperty2</Property>
    <Type>testType2</Type>
    <Validate>testValidate2</Validate>
  </control_Widget>
  <control_Widget>
    <Name>testName3</Name>
    <Value>testValue3</Value>
    <Property>testProperty3</Property>
    <Type>testType3</Type>
    <Validate>testValidate3</Validate>
  </control_Widget>
  </ControlList>
</TestProject>

这也算是今天的一个小难题,因为之前并没有真正意义上的解析过xml,所以今天在解析xml的时候,就频繁报错儿(本人数学不好,估计影响很大),后来在公司解决无果,就果断选择回家,Andy说一般反序列化解析xml,速度会相当快,所以路上就拿手机上网搜了下,简单的了解下,回家开始动手,就做了个这样儿的简单的Demo。

首先根据该XML文件的结构,设计了如下的实体类库:

    //Serializable class
    public class TestProject
    {
        public List<Control_Widget> ControlList = new List<Control_Widget>();
    }

    //Xml ArrayList
    public class Control_Widget
    {
        public string Name;
        public string Value;
        public string Property;
        public string Type;
        public string Validate;
    }

因为这样的话,我就基本不用对xml文档进行大规模改动(xml文件中数据不多不少,改起来也足够费劲的)。

OK,那么接下来,就应该按照国际惯例,开始写解析的方法:

XmlReader类部分代码

public List<Control_Widget> controlTotalList;//the list of Control_Widget
        string path = "C:\\C#Project\\ControlWidget.xml";

        //read xml file content,and add to List
        public void ReadXmlFile()
        {
            FileStream fs = null;
            TestProject testPrj = new TestProject();
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(TestProject));
                fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                testPrj = (TestProject)xs.Deserialize(fs);
                controlTotalList = new List<Control_Widget>();
                controlTotalList = testPrj.ControlList;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

经过这样的操作之后,xml文件中的内容就被解析出来并且存到类的局部变量controlTotalList集合中,当然,我要求的功能还有一步,就是要根据xml文件的Name标签中的内容来查询相对应的Value标签中的内容,那么,再添加如下的GetValueByName方法:

public string GetValueByName(string name)
        {
            string value = "";
            if (name != null && name != "")
            {
                if (controlTotalList != null)
                {
                    for (int i = 0; i < controlTotalList.Count; i++)
                    {
                        if (controlTotalList[i].Name == name)
                        {
                            value = controlTotalList[i].Value;
                        }
                    }
                }
                else
                {
                    ReadXmlFile();
                    GetValueByName(name);
                }
            }
            return value;
        }

OK,大功告成!一个基本的xml反序列化的例子就这样儿完成了。

写完博客,睡觉去了,晚安北京!加油明天!

抱歉!评论已关闭.