C#使用XmlSerializer读取Xml内容

1、book.xml文件如下

 

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.books.com">
  <book genre="asp.net" publicationdate="2012" ISBN="1-861003-11-0">
    <title>asp.net 本质论</title>
    <author>
      <firstname>刘</firstname>
      <lastname>湘</lastname>
    </author>
    <price>10.23</price>
  </book>
  <book genre="c#" publicationdate="2012" ISBN="2-861003-11-1">
    <title>c#基础</title>
    <author>
      <firstname>孟</firstname>
      <lastname>第</lastname>
    </author>
    <price>50.28</price>
  </book>
</bookstore> 

 

 2、根据book.xml文件生成架构文件book.xsd

在vs2010打开book.xml文件,vs2010的菜单栏上出现一个XML的菜单,选择“创建架构”,vs2010会自动生成book.xsd文件

 

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.books.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="bookstore">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="book">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" />
              <xs:element name="author">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="firstname" type="xs:string" />
                    <xs:element name="lastname" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="price" type="xs:decimal" />
            </xs:sequence>
            <xs:attribute name="genre" type="xs:string" use="required" />
            <xs:attribute name="publicationdate" type="xs:unsignedShort" use="required" />
            <xs:attribute name="ISBN" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

 

 3、根据book.xsd生成net CLR类型

使用vs2010的xsd.exe生成类,XML 架构定义工具 (Xsd.exe)可参看:http://msdn.microsoft.com/zh-cn/library/x6c1kb0s%28v=vs.80%29.aspx

步骤如下,

开始—> Microsoft Visual Studio 2010 —> Visual Studio Tools —>Visual Studio Command Prompt (2010)

如图

4、使用XmlSerializer 读取xml文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace XmlTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(bookstore));
            string bookXmlPath = System.AppDomain.CurrentDomain.BaseDirectory + "book.xml";
            using (FileStream fs = new FileStream(bookXmlPath, FileMode.Open))
            {
                bookstore books = (bookstore)serializer.Deserialize(fs);
                foreach (bookstoreBook book in books.book)
                {
                    Console.WriteLine(book.title);
                }
            }
            Console.ReadLine();
        }
          
    }
}

 

 

 

posted @ 2012-10-13 16:09  虎头  阅读(4236)  评论(0编辑  收藏  举报