zoukankan      html  css  js  c++  java
  • 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();
            }
              
        }
    }
    

     

     

  • 相关阅读:
    sql over(partition by) 开窗函数的使用
    利用curl函数处理GET数据获取微信公众号的access_token
    2018.4.12
    字段和属性
    C#实现回车键登录
    判断DataTable里面数据是否有重复数据
    一个强大的人民币大写转换的正则表达式
    C#将image中的显示的图片转换成二进制
    遍历Dev LayoutControl中的所有控件信息
    遍历窗体中所有控件的信息
  • 原文地址:https://www.cnblogs.com/50614090/p/2722606.html
Copyright © 2011-2022 走看看