青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Use XML Serialization with Custom Objects/Create a Schema for a .NET Class/Generate a Class from a Schema(XML 四)

Solution

Use the System.Xml.Serialization.XmlSerializer class to transfer data from your object to XML, and vice versa. You can also mark up your class code with attributes to customize its XML representation.

The only requirements for using XmlSerializer are as follows:

  • The XmlSerializer only serializes properties and public variables.

  • The classes you want to serialize must include a default zero-argument constructor. The XmlSerializer uses this constructor when creating the new object during deserialization.

  • All class properties must be readable and writable. This is because XmlSerializer uses the property get accessor to retrieve information, and the property set accessor to restore the data after deserialization.

To use XML serialization, you must first mark up your data objects with attributes that indicate the desired XML mapping. These attributes are found in the System.Xml.Serialization namespace and include the following:

  • XmlRoot Specifies the name of the root element of the XML file. By default, XmlSerializer will use the name of the class. This attribute can be applied to the class declaration.

  • XmlElement Indicates the element name to use for a property or public variable. By default, XmlSerializer will use the name of the property or public variable.

  • XmlAttribute Indicates that a property or public variable should be serialized as an attribute, not an element, and specifies the attribute name.

  • XmlEnum Configures the text that should be used when serializing enumerated values. If you don't use XmlEnum, the name of the enumerated constant will be used.

  • XmlIgnore Indicates that a property or public variable should not be serialized.

For example, consider the product catalog first shown in recipe 5.1. You can represent this XML document using ProductCatalog and Product objects. Here's the class code that you might use:

using System;
using System.Xml.Serialization;

[XmlRoot("productCatalog")]
public class ProductCatalog {

    [XmlElement("catalogName")]
    public string CatalogName;
    
    // Use the date data type (and ignore the time portion in the 
    // serialized XML).
    [XmlElement(ElementName="expiryDate", DataType="date")]
    public DateTime ExpiryDate;
    
    // Configure the name of the tag that holds all products,
    // and the name of the product tag itself.
    [XmlArray("products")]
    [XmlArrayItem("product")]
    public Product[] Products;

    public ProductCatalog() {
        // Default constructor for deserialization.
    }

    public ProductCatalog(string catalogName, DateTime expiryDate) {
        this.CatalogName = catalogName;
        this.ExpiryDate = expiryDate;
    }
}

public class Product {

    [XmlElement("productName")]
    public string ProductName;
    
    [XmlElement("productPrice")]
    public decimal ProductPrice;
    
    [XmlElement("inStock")]
    public bool InStock;
    
    [XmlAttributeAttribute(AttributeName="id", DataType="integer")]
    public string Id;

    public Product() {
        // Default constructor for serialization.
    }

    public Product(string productName, decimal productPrice) {
        this.ProductName = productName;
        this.ProductPrice = productPrice;
    }
}

Notice that these classes use the XML serialization attributes to rename element names (using Pascal casing in the class member names, and camel casing in the XML tag names), indicate data types that aren't obvious, and specify how <product> elements will be nested in the <productCatalog>.

Using these custom classes and the XmlSerializer object, you can translate XML into objects and vice versa. Here's the code you would need to create a new ProductCatalog object, serialize the results to an XML document, deserialize the document back to an object, and then display the XML document.

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

public class SerializeXml {

    private static void Main() {

        // Create the product catalog.
        ProductCatalog catalog = new ProductCatalog("New Catalog",
          DateTime.Now.AddYears(1));
        Product[] products = new Product[2];
        products[0] = new Product("Product 1", 42.99m);
        products[1] = new Product("Product 2", 202.99m);
        catalog.Products = products;

        // Serialize the order to a file.
        XmlSerializer serializer = new XmlSerializer(typeof(ProductCatalog));
        FileStream fs = new FileStream("ProductCatalog.xml", FileMode.Create);
        serializer.Serialize(fs, catalog);
        fs.Close();

        catalog = null;

        // Deserialize the order from the file.
        fs = new FileStream("ProductCatalog.xml", FileMode.Open);
        catalog = (ProductCatalog)serializer.Deserialize(fs);

        // Serialize the order to the Console window.
        serializer.Serialize(Console.Out, catalog);
        Console.ReadLine();
    }
}
----------------------------------------------------

Solution

Use the XML Schema Definition Tool (xsd.exe) command-line utility included with the .NET Framework.
Specify the name of your assembly as a command- line argument, and add the /t:[TypeName]
parameter to indicate the types you want to convert.

The xsd.exe utility is included with the .NET Framework. If you've installed Microsoft Visual Studio .NET,
you'll find it in a directory like C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin.
The xsd.exe utility can generate schema documents from compiled assemblies. You simply need to supply the
filename and indicate the class that represents the XML document with the /t:[TypeName] parameter.

For example, consider the ProductCatalog and Product classes shown in recipe 5.9.(即XML 三) You could create the
XML schema for a product catalog with the following command line:


xsd Recipe5-09.exe /t:ProductCatalog

You need to specify only the ProductCatalog class on the command line because this class represents the actual
XML document. The generated schema in this example will represent a complete product catalog,
with contained product items. It will be given the default filename schema0.xsd.

--------------------------------------------------------------

Solution

Use the xsd.exe command-line utility included with the .NET Framework. Specify the name of your schema file
as a command-line argument, and add the /c parameter to indicate that you want to generate class code.


To generate source code from a schema, you simply need to supply the filename of the schema document
and add the /c parameter to indicate that you want to generate the required classes. For example,
consider the schema shown in recipe 5.8. (上面所產生的xsd)You can generate C# code for this schema with the following command-line:

xsd ProductCatalog.xsd /c

This will generate one file (ProductCatalog.cs) with two classes: product and productCalalog. These classes
are similar to the ones created in recipe 5.9,(上面所寫的constom object中) except for the fact that the class member names match the
XML document exactly.



posted on 2005-11-23 18:40 夢在天涯 閱讀(908) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816732
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲理伦电影| 久久成人综合视频| 欧美巨乳在线| 一区二区欧美在线观看| 亚洲精品中文字幕有码专区| 欧美日韩mp4| 亚洲欧美久久久| 亚洲欧美激情四射在线日| 国产亚洲精品自拍| 老牛嫩草一区二区三区日本 | 日韩视频一区二区三区| 亚洲激情小视频| 欧美日韩亚洲综合在线| 午夜亚洲福利| 久久综合九色99| 夜夜嗨av一区二区三区四季av| 夜夜夜久久久| 国语自产精品视频在线看抢先版结局| 免费在线播放第一区高清av| 欧美国产综合| 欧美在线视频观看免费网站| 久久久久中文| 亚洲一区二区成人在线观看| 欧美人交a欧美精品| 欧美国产综合视频| 鲁鲁狠狠狠7777一区二区| 国产精品视频内| 亚洲激情欧美| 亚洲一二三级电影| 尤物视频一区二区| 一本大道久久精品懂色aⅴ| 国产精品视屏| 亚洲国产一成人久久精品| 国产农村妇女精品一二区| 牛人盗摄一区二区三区视频| 欧美日韩国产系列| 毛片精品免费在线观看| 国产精品大全| 欧美成年人视频| 国产拍揄自揄精品视频麻豆| 亚洲精品视频一区| 伊人一区二区三区久久精品| 亚洲午夜精品17c| 日韩亚洲欧美成人| 久久夜色精品国产欧美乱极品| 亚洲一区二区免费| 欧美精品久久99| 欧美成人资源| 韩国一区二区三区美女美女秀| 中日韩美女免费视频网址在线观看| 亚洲电影免费观看高清完整版在线观看 | 午夜电影亚洲| 亚洲一区二区在线看| 牛夜精品久久久久久久99黑人| 欧美在线播放视频| 国产精品日韩欧美一区| 亚洲精品日韩一| 亚洲欧洲精品一区| 久久久噜噜噜久久久| 久久精品国产一区二区三区免费看 | 亚洲欧美日韩直播| 亚洲性感激情| 欧美精选午夜久久久乱码6080| 免费在线日韩av| 伊人色综合久久天天| 久久国产精品久久久久久久久久| 午夜精品久久| 国产精品福利片| 国产精品99久久久久久有的能看| 中文亚洲视频在线| 欧美午夜美女看片| 99www免费人成精品| 亚洲视频精品在线| 欧美日韩国产色视频| 亚洲伦伦在线| 亚洲一品av免费观看| 国产精品美女久久久久av超清| 中文精品99久久国产香蕉| 亚洲一区国产视频| 国产精品亚洲综合一区在线观看| 亚洲一区免费网站| 久久久噜噜噜久久狠狠50岁| 国内外成人免费激情在线视频| 久久免费一区| 亚洲欧洲精品一区二区三区| 一本色道久久综合狠狠躁篇怎么玩| 欧美日韩免费一区| 亚洲一区视频在线| 久久深夜福利| 亚洲清纯自拍| 国产精品久久7| 久久精品视频免费播放| 亚洲国产欧美久久| 亚洲免费影视| 精品白丝av| 欧美日韩国产天堂| 亚洲欧美另类中文字幕| 久热国产精品| 亚洲视频你懂的| 激情文学一区| 欧美日韩亚洲一区| 久久久精品2019中文字幕神马| 91久久久精品| 久久精品国产一区二区电影| 91久久黄色| 国产精品毛片高清在线完整版| 久久人体大胆视频| 亚洲视频久久| 亚洲国产综合91精品麻豆| 午夜精品在线看| 亚洲麻豆av| 黄色日韩精品| 国产精品国产自产拍高清av王其| 久久亚洲午夜电影| 亚洲一区网站| 亚洲精选中文字幕| 免费成人av在线看| 亚洲欧美日韩久久精品| 亚洲激情中文1区| 国产网站欧美日韩免费精品在线观看| 欧美成人免费观看| 欧美一区二区三区在线免费观看| 亚洲精品乱码久久久久久日本蜜臀| 久久精品国产久精国产一老狼| 一本到高清视频免费精品| 一区二区三区在线视频观看 | 久久成年人视频| 亚洲香蕉在线观看| 亚洲精品一区二区三| 欧美成人一区二免费视频软件| 久久精品2019中文字幕| 亚洲无线一线二线三线区别av| 亚洲狠狠丁香婷婷综合久久久| 狠色狠色综合久久| 国产日韩成人精品| 国产精品一级在线| 欧美亚一区二区| 欧美色大人视频| 欧美巨乳在线观看| 欧美精品一区二区三区久久久竹菊 | 玖玖玖国产精品| 久久精品一区二区三区不卡| 性久久久久久| 欧美一级大片在线观看| 亚洲伊人第一页| 亚洲欧美在线x视频| 亚洲影院在线观看| 亚洲男人的天堂在线| 亚洲夜间福利| 亚洲中无吗在线| 午夜久久久久久| 欧美亚洲一级片| 久久精品亚洲国产奇米99| 欧美主播一区二区三区| 久久xxxx精品视频| 久久久精品国产免大香伊 | 一本不卡影院| 亚洲小少妇裸体bbw| 亚洲在线中文字幕| 久久精品91| 美女啪啪无遮挡免费久久网站| 美脚丝袜一区二区三区在线观看| 欧美成人免费一级人片100| 欧美激情一区二区三区不卡| 亚洲国产精品成人综合色在线婷婷| 亚洲激情校园春色| 一区二区三区导航| 欧美一区中文字幕| 免费观看成人网| 欧美日韩一卡| 国产欧美日本在线| 亚洲国产精品电影| 亚洲一区二区三区欧美| 久久国产夜色精品鲁鲁99| 卡一卡二国产精品| 亚洲人成亚洲人成在线观看图片| 这里只有精品丝袜| 久久久精品久久久久| 欧美日本在线播放| 国产精品亚发布| 最新亚洲视频| 欧美一区二区三区免费观看| 免费不卡欧美自拍视频| 日韩香蕉视频| 久久亚洲风情| 国产欧美日本一区视频| 亚洲人成7777| 久久高清一区| 日韩一区二区福利| 久久久久久亚洲精品中文字幕 | 午夜精品视频一区| 欧美国产激情| 国产香蕉97碰碰久久人人| 亚洲人体一区| 久久综合五月天婷婷伊人| 在线亚洲一区观看| 欧美福利电影网| 一区二区三区中文在线观看| 亚洲欧美视频在线| 亚洲日本欧美日韩高观看|