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

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

搜索

  •  

積分與排名

  • 積分 - 1817650
  • 排名 - 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>
              宅男在线国产精品| 国产一区二区精品久久| 亚洲国产成人在线播放| 美女91精品| 久久综合九色综合网站| 亚洲欧洲日韩在线| 亚洲伦伦在线| 国产精品视频1区| 久热精品在线视频| 男同欧美伦乱| 亚洲在线免费观看| 午夜精品久久久久久99热软件| 国产一区二区无遮挡| 蜜臀久久99精品久久久画质超高清| 老司机成人网| 亚洲午夜免费视频| 欧美在线视频免费| 日韩天堂在线观看| 午夜视频久久久久久| 亚洲国产精品ⅴa在线观看| 日韩视频一区二区| 国产综合色产在线精品| 欧美国产日本在线| 国产伦精品一区二区三区在线观看 | 国产精品一区二区三区久久| 久久久久久久999| 欧美精品久久久久久久久老牛影院 | 亚洲高清在线观看| 日韩一级在线观看| 国内精品一区二区三区| 亚洲黄色毛片| 国产精品日韩电影| 欧美激情在线| 国产欧美韩日| 日韩视频免费在线| **欧美日韩vr在线| 亚洲视频在线二区| 91久久精品日日躁夜夜躁国产| 亚洲特级片在线| 91久久精品日日躁夜夜躁欧美| 一区二区免费看| 亚洲电影在线免费观看| 在线视频精品一区| 亚洲国产人成综合网站| 一区二区三区回区在观看免费视频| 精久久久久久久久久久| 亚洲小视频在线观看| 99精品欧美一区二区蜜桃免费| 久久成人人人人精品欧| 亚洲欧美视频在线观看| 欧美精品电影| 亚洲二区在线视频| 一区二区三区自拍| 欧美亚洲视频| 香蕉久久精品日日躁夜夜躁| 欧美精品一线| 亚洲激情视频在线| 亚洲国产视频直播| 久久人人爽人人爽爽久久| 久久久91精品国产一区二区三区 | 久久久久9999亚洲精品| 久久福利影视| 国产精品视频免费在线观看| 日韩视频永久免费观看| 亚洲理论电影网| 你懂的一区二区| 欧美国产日韩一二三区| 在线免费观看成人网| 久久久久久久久伊人| 久久伊人免费视频| 1204国产成人精品视频| 久久综合九色综合网站| 亚洲成人在线视频播放 | 免费看成人av| 亚洲电影免费| 可以看av的网站久久看| 亚洲成色999久久网站| 亚洲精品孕妇| 欧美少妇一区二区| 亚洲午夜羞羞片| 久久成人18免费网站| 国产亚洲福利社区一区| 久久精品人人爽| 欧美激情 亚洲a∨综合| 99re这里只有精品6| 欧美日韩午夜激情| 亚洲欧美日韩天堂一区二区| 久久久久综合| 亚洲蜜桃精久久久久久久| 欧美日韩国产精品一卡| 亚洲综合第一页| 久久免费的精品国产v∧| 亚洲国产精品成人精品| 欧美日韩激情小视频| 午夜精品久久久久| 欧美激情国产日韩| 亚洲综合国产| 亚洲第一成人在线| 欧美少妇一区二区| 久久久久久久综合色一本| 亚洲国产人成综合网站| 亚洲在线视频一区| 在线观看欧美| 国产精品videosex极品| 久久国产福利| 亚洲精品国产精品乱码不99| 欧美一区二区三区四区高清| 亚洲日本电影在线| 国产美女扒开尿口久久久| 欧美激情成人在线| 久久久91精品国产一区二区三区 | 亚洲激情成人在线| 欧美一级视频免费在线观看| 亚洲经典一区| 国产午夜精品美女毛片视频| 欧美日韩国产一区精品一区 | 欧美电影免费观看网站| 亚洲欧美中日韩| 亚洲理论电影网| 欧美77777| 久久久久久亚洲精品中文字幕 | 亚洲视频久久| 在线播放豆国产99亚洲| 国产精品日韩电影| 欧美性大战久久久久久久蜜臀| 久久久亚洲国产天美传媒修理工| 亚洲深夜福利在线| 日韩视频免费观看高清完整版| 嫩草成人www欧美| 久久精品国产96久久久香蕉| 亚洲自拍偷拍一区| 一区二区三区视频在线观看| 亚洲二区免费| 在线观看视频一区二区欧美日韩| 国产精一区二区三区| 国产精品swag| 国产精品国产馆在线真实露脸| 欧美日韩国产在线观看| 欧美成人精品一区二区三区| 久久亚洲视频| 久久影视三级福利片| 久久久久久久一区二区| 久久久久久久久久码影片| 欧美一区二区在线视频| 欧美亚洲三级| 久久精品女人的天堂av| 久久久国产午夜精品| 久久久高清一区二区三区| 久久国产免费| 理论片一区二区在线| 毛片精品免费在线观看| 免费欧美在线视频| 欧美激情性爽国产精品17p| 欧美激情久久久久久| 欧美精品一区二区精品网 | 欧美国产第一页| 欧美精品在欧美一区二区少妇| 欧美日本韩国在线| 欧美性猛交99久久久久99按摩| 国产精品午夜久久| 国产日产亚洲精品系列| 一色屋精品视频在线看| 亚洲三级视频| 亚洲综合导航| 久久久久久久久久看片| 欧美国产专区| 日韩视频在线观看免费| 亚洲欧美另类中文字幕| 久久在线播放| 欧美日韩国产在线一区| 国产欧美一区二区视频| 亚洲大片精品永久免费| 亚洲一区二区三区精品在线观看| 久久国产福利| 亚洲第一中文字幕在线观看| 亚洲视频在线观看免费| 久久天天综合| 欧美亚州一区二区三区| 黄色亚洲在线| 亚洲视频在线观看三级| 久久久久久久波多野高潮日日| 亚洲高清免费| 午夜老司机精品| 欧美破处大片在线视频| 国产在线精品二区| 中文有码久久| 欧美大片免费久久精品三p | 蜜桃久久av| 国产精品区一区| 亚洲日本一区二区三区| 久久精品国产69国产精品亚洲| 欧美激情片在线观看| 欧美一区二区视频97| 欧美日韩精品在线观看| 影音先锋另类| 欧美一区二区三区免费大片| 亚洲美女黄网| 久久一日本道色综合久久| 国产伦精品一区二区三区高清| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 |