• <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>

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

            搜索

            •  

            積分與排名

            • 積分 - 1807508
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久天天躁狠狠躁夜夜avapp| 久久天天躁狠狠躁夜夜躁2O2O | 久久久久久夜精品精品免费啦| 久久人人添人人爽添人人片牛牛| 亚洲国产精品无码久久久不卡| 国内精品久久久久久久97牛牛| 国产精品美女久久久久AV福利| 色欲综合久久躁天天躁| 久久精品一本到99热免费| 精品综合久久久久久888蜜芽| 久久亚洲中文字幕精品一区| 久久亚洲AV成人无码电影| 亚洲а∨天堂久久精品| 久久不见久久见免费视频7| 欧美激情精品久久久久久久九九九| 97精品国产97久久久久久免费 | 99久久做夜夜爱天天做精品| 夜夜亚洲天天久久| 99精品久久精品一区二区| 亚洲精品无码专区久久久| 久久精品视频91| 亚洲国产天堂久久综合网站| 日韩人妻无码一区二区三区久久 | 久久亚洲欧美国产精品| 亚洲午夜无码AV毛片久久| 91精品婷婷国产综合久久| 久久国产精品-久久精品| 亚洲综合日韩久久成人AV| 久久久久久国产a免费观看黄色大片| 国产91久久精品一区二区| 久久精品亚洲一区二区三区浴池| 99久久香蕉国产线看观香| 国内精品伊人久久久久妇| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久久精品国产亚洲成人满18免费网站 | 伊人久久一区二区三区无码| 久久高清一级毛片| 久久午夜福利电影| 久久99这里只有精品国产| 热久久视久久精品18| 18禁黄久久久AAA片|