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

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

搜索

  •  

積分與排名

  • 積分 - 1811733
  • 排名 - 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>
              亚洲欧美久久久| 亚洲一区二区三区四区五区午夜| 国产日本欧美一区二区三区| 免费不卡视频| 性欧美大战久久久久久久免费观看 | 在线观看精品一区| 国产精品视区| 国产精品黄视频| 欧美色图五月天| 欧美视频网址| 欧美日韩天堂| 欧美精品在欧美一区二区少妇| 美女亚洲精品| 欧美成人日韩| 欧美久久电影| 欧美日韩一区三区| 欧美午夜视频在线观看| 久久蜜桃资源一区二区老牛 | 久久久欧美精品sm网站| 亚洲影视在线播放| 亚洲自啪免费| 午夜亚洲福利在线老司机| 亚洲欧美色婷婷| 欧美在线视频观看| 亚洲制服欧美中文字幕中文字幕| 一区二区国产精品| 亚洲婷婷免费| 亚洲毛片av在线| 亚洲深夜福利视频| 午夜国产精品视频| 久久深夜福利| 亚洲大胆人体在线| 欧美成人首页| 亚洲精选久久| 亚洲日韩中文字幕在线播放| 久久精品一区二区国产| 久久夜色精品国产| 一本久道综合久久精品| 老鸭窝91久久精品色噜噜导演| 国产精品白丝黑袜喷水久久久| 亚洲国产精品尤物yw在线观看| 羞羞答答国产精品www一本| 亚洲第一综合天堂另类专| 欧美一级精品大片| 国产精品久久999| 亚洲精品欧美精品| 狂野欧美激情性xxxx欧美| 亚洲小说欧美另类社区| 欧美日本一道本在线视频| 亚洲激情小视频| 免费亚洲一区二区| 久久爱www久久做| 国产精品系列在线| 亚洲欧美影音先锋| 一本久久精品一区二区| 欧美激情成人在线| 亚洲欧洲在线视频| 欧美国产激情| 麻豆精品精品国产自在97香蕉| 激情六月婷婷久久| 久久在线免费观看| 久久伊伊香蕉| 亚洲人成绝费网站色www| 亚洲第一视频| 欧美精品免费看| 一区二区三区视频在线| 99精品热视频| 国产精品视区| 久久精品国语| 久久综合久久美利坚合众国| 亚洲成人在线免费| 欧美大片专区| 欧美福利视频在线观看| 日韩天堂av| 欧美大片一区二区三区| 影音先锋久久资源网| 久久视频精品在线| 久久精品官网| 亚洲国产精品视频一区| 亚洲高清色综合| 欧美日韩1区2区3区| 亚洲桃色在线一区| 久久av一区二区| 亚洲美女在线视频| 亚洲天堂av电影| 国内外成人在线| 欧美激情区在线播放| 欧美日韩一区不卡| 久久精品亚洲一区二区| 老司机67194精品线观看| 一卡二卡3卡四卡高清精品视频| 在线视频欧美日韩精品| 国产亚洲欧美一区二区三区| 欧美国产日韩精品免费观看| 欧美日韩免费网站| 久久狠狠婷婷| 欧美黄在线观看| 欧美一区二区三区精品| 欧美aa国产视频| 亚洲欧美日本国产专区一区| 久久色在线观看| 亚洲欧美韩国| 久久久美女艺术照精彩视频福利播放| 亚洲精品综合精品自拍| 欧美一级视频| 亚洲精品护士| 久久国产免费看| 亚洲一卡二卡三卡四卡五卡| 久久精品五月| 亚洲欧美综合v| 欧美激情第3页| 老司机免费视频久久| 欧美性猛交视频| 亚洲国产日本| 精品99一区二区三区| 亚洲一区二区三区777| 亚洲欧洲一级| 久久国产精品99久久久久久老狼| 一本色道久久88综合亚洲精品ⅰ| 欧美一区二区三区日韩视频| 亚洲性感美女99在线| 欧美黑人在线播放| 欧美激情成人在线| 激情欧美国产欧美| 久久www成人_看片免费不卡| 亚洲在线观看免费| 欧美伦理在线观看| 亚洲丰满在线| 亚洲韩国日本中文字幕| 久久成年人视频| 久久久久国产精品厨房| 国产精品性做久久久久久| aa国产精品| 亚洲乱码国产乱码精品精98午夜| 久久国产综合精品| 久久精品夜夜夜夜久久| 国产亚洲欧美日韩美女| 久久av二区| 久久香蕉国产线看观看av| 国产精品乱人伦一区二区| 国产精品久久久久永久免费观看 | 欧美激情视频给我| 亚洲国产aⅴ天堂久久| 亚洲精品久久久久中文字幕欢迎你 | 久久久国产精品亚洲一区 | 欧美三级午夜理伦三级中文幕| 亚洲高清在线| 亚洲美女av黄| 欧美另类一区二区三区| 亚洲免费高清视频| 午夜精品久久久久久久久久久久久| 欧美色欧美亚洲另类二区| 99精品视频免费观看视频| 亚洲淫性视频| 国产午夜精品视频| 久久一区亚洲| 亚洲精品国产精品国自产在线 | 亚洲精选国产| 欧美日韩一区在线观看| 亚洲一级在线观看| 久久精品欧美日韩| 亚洲福利免费| 欧美日韩国产天堂| 性欧美在线看片a免费观看| 久久久久久久999精品视频| 亚洲第一区在线| 欧美日韩中文精品| 欧美一区二区三区免费看 | 久久亚洲欧洲| 亚洲国产日韩在线| 欧美日韩视频一区二区三区| 亚洲欧美另类国产| 欧美激情在线观看| 小嫩嫩精品导航| 亚洲国产欧美不卡在线观看| 欧美三级在线播放| 久久午夜av| 一区二区三区偷拍| 美女性感视频久久久| 亚洲永久字幕| 亚洲国产精品久久久久| 国产精品黄色在线观看| 暖暖成人免费视频| 亚洲欧美在线观看| 91久久精品美女高潮| 久久精品国产999大香线蕉| 亚洲经典在线| 国产婷婷色一区二区三区在线| 免费观看日韩| 欧美伊人影院| 亚洲国产精品成人| 国产精品v欧美精品v日本精品动漫| 99视频精品全部免费在线| 久久久久久穴| 亚洲免费在线看| 久久九九国产| 夜夜嗨av色一区二区不卡| 亚洲免费小视频| 亚洲黄色免费电影| 国内精品久久久久影院优|