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

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 夢在天涯 閱讀(912) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1818827
  • 排名 - 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>
              亚洲精品视频在线观看免费| 狠久久av成人天堂| 欧美视频中文字幕| 国产精品一区二区久久| 中国成人黄色视屏| 一本久道综合久久精品| 欧美亚洲免费在线| 久久青草欧美一区二区三区| 欧美国产激情| 久久久噜噜噜久久| 亚洲自拍偷拍福利| 日韩视频―中文字幕| 亚洲欧美国产高清| 欧美激情精品久久久久久蜜臀| 国产精品欧美久久久久无广告| 午夜久久久久| 欧美精品一区在线发布| 国产日韩亚洲欧美精品| 亚洲人成人一区二区在线观看| 在线观看亚洲| 国产乱码精品一区二区三区五月婷 | 亚洲图片在线| 久久福利电影| 91久久精品国产| 日韩一级黄色片| 久久久久青草大香线综合精品| 欧美一级一区| 亚洲三级免费电影| 免费在线视频一区| 国产综合av| 久久国产精品一区二区三区四区 | 91久久久久久久久久久久久| 亚洲国产高清自拍| 亚洲精品视频一区| 欧美不卡视频一区| 91久久久久久| 亚洲男人的天堂在线观看 | 久久这里有精品15一区二区三区| 久久只精品国产| 欧美日韩国产在线播放网站| 亚洲精品乱码久久久久久日本蜜臀| 99精品久久| 欧美一区二区免费| 国模精品一区二区三区| 久久久久国产精品一区二区| 午夜精品三级视频福利| 亚洲老板91色精品久久| 亚洲高清精品中出| 韩国成人精品a∨在线观看| 亚洲伦理网站| 亚洲精品一区在线观看| 欧美在线一二三| 欧美日韩国语| 亚洲性人人天天夜夜摸| 亚洲精品久久久久| 亚洲国产成人午夜在线一区| 亚洲女人天堂av| 亚洲视频网在线直播| 亚洲日本中文字幕| 蜜桃av噜噜一区二区三区| 亚洲日本一区二区| 久久色在线播放| 久久蜜桃精品| 欧美韩日高清| 欧美激情第10页| 欧美日韩在线不卡一区| 欧美中文在线视频| 国产精品成人一区二区艾草| 久久米奇亚洲| 狠狠色狠狠色综合日日小说| 午夜视频精品| 久久久亚洲成人| 黄色成人在线网址| 六月婷婷一区| 亚洲茄子视频| 亚洲午夜精品久久| 久久久久久久一区二区三区| 亚洲精品午夜| 欧美日本国产在线| 亚洲精品影视| 怡红院精品视频| 久久天天躁狠狠躁夜夜av| 欧美成人亚洲| 国产精品免费网站在线观看| 亚洲一区二区三区涩| 亚洲国产日韩欧美综合久久| 在线视频亚洲欧美| 久久成人精品电影| 狠狠色丁香婷婷综合久久片| 免费欧美日韩国产三级电影| 亚洲三级影片| 欧美在线视频一区二区三区| 精品不卡在线| 欧美一级欧美一级在线播放| 久久久免费观看视频| 亚洲国产精品va在线看黑人动漫| 亚洲网站在线| 亚洲精品一品区二品区三品区| 亚洲欧美日本国产专区一区| 黄色成人免费网站| 欧美成人资源| 亚洲综合清纯丝袜自拍| 免费观看成人网| 国产在线拍揄自揄视频不卡99| 日韩亚洲欧美精品| 久久久久国色av免费看影院| 亚洲电影下载| 久久精品夜色噜噜亚洲aⅴ| 午夜精品三级视频福利| 欧美日韩专区在线| 欧美一区久久| 日韩一区二区精品视频| 久久精品噜噜噜成人av农村| 亚洲精品美女在线观看| 国产精品亚洲精品| 欧美激情精品久久久久久蜜臀 | 亚洲毛片视频| 国产一区二区高清不卡| 香蕉久久夜色精品国产| 亚洲欧美日韩精品在线| 欧美丝袜第一区| 美女国内精品自产拍在线播放| 欧美成人精品在线播放| 伊人成年综合电影网| 国产精品久久久久久影视| 欧美aⅴ99久久黑人专区| 欧美大秀在线观看| 久久影音先锋| 久久国产精品亚洲77777| 亚洲一区二区少妇| 国产精品一区二区在线观看| 免费久久99精品国产自| 久久精品国产99国产精品| 亚洲午夜在线| 亚洲精品国产无天堂网2021| 尤物九九久久国产精品的特点 | 亚洲精品国产精品国自产在线| 一区二区三区高清视频在线观看| 亚洲综合色激情五月| 亚洲精品三级| 亚洲高清资源| 亚洲国产高清自拍| 亚洲国产天堂久久综合网| 国产一区亚洲| 国产欧美日韩一区| 美女网站久久| 在线亚洲免费视频| 日韩网站在线看片你懂的| 亚洲精品欧洲精品| 亚洲日本欧美| 久久婷婷av| 久久亚洲私人国产精品va| 久久久久久伊人| 久久全球大尺度高清视频| 一区二区三欧美| 夜色激情一区二区| 欧美日韩视频免费播放| 欧美精品一区在线播放| 欧美日韩综合精品| 国产麻豆精品theporn| 久色婷婷小香蕉久久| 亚洲网站在线看| 欧美一区三区三区高中清蜜桃| 亚洲第一页自拍| 91久久精品国产91久久性色tv| 久久久久久久一区二区| 麻豆91精品| 久久国产精品久久久| 久久这里只有| 亚洲人成人一区二区在线观看| 久久婷婷亚洲| 亚洲第一中文字幕在线观看| 亚洲精品在线视频观看| 亚洲一级在线观看| 亚洲老板91色精品久久| 亚洲影院在线观看| 久久久人成影片一区二区三区| 亚洲欧美日韩网| 久久野战av| 欧美色图天堂网| 一区二区三区在线视频观看| 国产精品区一区二区三区| 欧美精品一卡| 欧美精品久久久久a| 国产精品r级在线| 国产精品免费在线| 国产精品久久二区| 国产精品高潮呻吟久久| 一区二区视频免费完整版观看| 国产欧美精品在线播放| 亚洲国产精品久久久久婷婷884 | 国产精品素人视频| 影音先锋久久精品| 亚洲欧美日韩第一区| 欧美aⅴ一区二区三区视频| 亚洲性色视频| 欧美激情中文字幕一区二区 | 亚洲欧美日韩另类| 欧美成人黑人xx视频免费观看| 美女网站久久|