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

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

搜索

  •  

積分與排名

  • 積分 - 1812202
  • 排名 - 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一区二区三区| 国产午夜一区二区三区| 一区二区av在线| 最新国产成人在线观看| 久久亚洲欧美| 欧美大片免费久久精品三p| 欧美韩日视频| 亚洲精品少妇30p| 一区二区日本视频| 久久精品视频在线观看| 欧美波霸影院| 国产精品久久久久91| 国外成人网址| 亚洲日本欧美日韩高观看| 日韩亚洲综合在线| 欧美一级二区| 亚洲激情视频网站| 亚洲小说欧美另类社区| 鲁鲁狠狠狠7777一区二区| 欧美日韩国产区一| 国内成人精品视频| 夜夜狂射影院欧美极品| 久久精品人人做人人爽电影蜜月| 久热综合在线亚洲精品| 亚洲免费播放| 欧美一区网站| 欧美四级剧情无删版影片| 国产亚洲亚洲| 亚洲天堂视频在线观看| 美女视频黄免费的久久| 亚洲午夜精品久久久久久app| 欧美一区二区三区视频在线| 免费观看在线综合| 国产亚洲免费的视频看| 宅男噜噜噜66一区二区66| 久久影院午夜片一区| 制服诱惑一区二区| 欧美极品在线观看| 樱桃视频在线观看一区| 午夜精品久久久久久久久久久| 欧美激情一二区| 久久精品观看| 国产精品一区一区三区| 一区二区欧美日韩| 欧美激情一区二区三区高清视频| 亚洲免费中文字幕| 久久精品一区四区| 亚洲美女av电影| 久久深夜福利| 新67194成人永久网站| 欧美日韩另类在线| 亚洲欧洲日本专区| 免费不卡在线观看av| 久久国产精品一区二区三区| 欧美日韩在线播放一区| 99综合视频| 亚洲精品日韩精品| 欧美高清一区| 99国产精品久久| 亚洲国产精品成人精品| 久久网站热最新地址| 在线播放精品| 欧美激情va永久在线播放| 久久人人看视频| 在线视频成人| 欧美激情精品久久久久久蜜臀| 老司机免费视频一区二区三区| 国产日韩欧美在线一区| 久久久噜噜噜| 久久久久久成人| 亚洲人成欧美中文字幕| 亚洲精品少妇网址| 国产精品久久久久秋霞鲁丝| 午夜精品久久久久久99热软件| 亚洲影院在线| 亚洲成人资源网| 亚洲日本免费| 国产精品亚洲综合色区韩国| 久久久久久999| 久久综合色8888| 中日韩美女免费视频网站在线观看| 亚洲美女诱惑| 国模吧视频一区| 欧美承认网站| 欧美乱在线观看| 欧美一级淫片aaaaaaa视频| 久久精品人人| 一本到高清视频免费精品| 亚洲一二三级电影| 在线观看国产欧美| 亚洲另类在线一区| 国产一区清纯| 日韩天堂在线视频| 国产伦精品一区二区三区视频孕妇 | 激情综合自拍| 亚洲国产精品黑人久久久| 欧美午夜美女看片| 美日韩精品免费观看视频| 欧美日韩国产综合在线| 久久久青草青青国产亚洲免观| 欧美日本一道本在线视频| 性色av一区二区三区| 欧美v国产在线一区二区三区| 性伦欧美刺激片在线观看| 久久夜色精品国产噜噜av| 欧美一二三区精品| 欧美日韩视频不卡| 一区二区三区色| 亚洲黄色成人网| 亚洲欧美日韩综合一区| 日韩一级精品| 久久夜色精品一区| 久久久久www| 国产精品成人免费| 欧美激情视频网站| 韩日欧美一区二区| 欧美在线二区| 欧美一级片一区| 欧美视频国产精品| 亚洲国产中文字幕在线观看| 精品粉嫩aⅴ一区二区三区四区| 一区二区日本视频| 日韩一级精品| 欧美金8天国| 欧美电影打屁股sp| 亚洲丁香婷深爱综合| 久久精品日产第一区二区三区| 欧美一区免费视频| 国产精品日韩在线一区| 99国产一区二区三精品乱码| 日韩视频免费观看高清完整版| 久久久www| 麻豆免费精品视频| 亚洲成人在线| 男人插女人欧美| 亚洲国产精品小视频| 日韩一二三在线视频播| 欧美激情一区二区三区在线视频观看 | 欧美日本国产精品| 亚洲级视频在线观看免费1级| 在线看片第一页欧美| 麻豆精品网站| 欧美激情亚洲一区| 一个人看的www久久| 欧美日韩伦理在线| 亚洲性夜色噜噜噜7777| 欧美一级专区| 狠狠色综合网站久久久久久久| 久久午夜视频| 亚洲国产经典视频| 一本一本久久a久久精品综合妖精| 欧美激情一区二区三区在线 | 久久久久一区二区三区| 久久久夜夜夜| 亚洲美女黄网| 国产精品一区在线播放| 欧美中文字幕视频| 欧美福利视频网站| 亚洲一二三区在线| 国产欧美一区二区精品婷婷| 久久九九99| 日韩午夜免费| 久久精品一区二区三区中文字幕 | 一区二区三区成人| 久久久国产精品一区二区中文| 国产精品女人久久久久久| 亚洲欧美一区二区三区久久 | 亚洲美女免费精品视频在线观看| 国内外成人免费视频| 亚洲欧美日韩国产一区| 亚洲激情偷拍| 欧美日韩国产一级片| 一本色道久久88综合亚洲精品ⅰ| 一区二区三区四区国产| 国产精品久久91| 欧美一区二区啪啪| 欧美成人在线影院| 中国女人久久久| 国产精品一级| 美日韩精品视频免费看| 亚洲视频在线视频| 亚洲丁香婷深爱综合| 亚洲福利国产| 国产精品一区久久久久| 欧美国产一区二区在线观看 | 欧美成人综合网站| 午夜在线播放视频欧美| 亚洲精选91| 国产一区视频在线观看免费| 欧美日本韩国在线| 久久综合色播五月| 香蕉免费一区二区三区在线观看 | 老司机久久99久久精品播放免费| 在线视频欧美一区| 韩国成人精品a∨在线观看| 欧美视频中文一区二区三区在线观看| 久久九九国产| 午夜精品美女自拍福到在线| 亚洲三级免费观看| 欧美~级网站不卡|