The release number of this article has been CHS301223
This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.
For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.
This task content
summary
How to read objects to XML reference
SUMMARY This article demonstrates how to use an XML serialization class to automatically map a scalable markup language (XML) stream to a set of objects used to save XML.
Back to top
The following table summarizes the recommended hardware, software, network architecture, and service pack required:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET Xsd.exe tools (including Visual Studio .NET and the .Net Framework SDK, available in the / Program Files / Microsoft.NET / Frameworksdk / bin or similar location to find this file) This article assumes that you are familiar with the following topics:
Serialization concept creates and read XML
Back to top
How to write objects to XML
Create a serialized class or create a class for processing an XML serialization class. If you have an XML schema definition (XSD) describing the XML file format to be loaded or saved, use the XSD.exe tool to create these classes. You can also manually create these classes. To create a serialized class using the XSD.exe tool, follow these steps:
Save the XSD architecture of the following description order as Po.xsd file: ElementFormDefault = "Qualified" Targetnamespace = ""> " Purchase Order Schema for Example.com. Copyright 2000 Example.com. All Rights Reserved. xsd: documentation> xsd: annotation> xsd: complexType> xsd: sequence> xsd: complexType> xsd: restriction> xsd: SimpleType> xsd: element> xsd: sequence> xsd: complexType> xsd: Element> xsd: sequence> xsd: complexType> xsd: restriction> xsd: SimpleType> xsd: schema> Generate class files using the XSD.exe tool. For example, the following command line code generates a C # class: XSD / CLASS / LANGUAGE: C # Po.xsdxsd.exe Tool When used with the schema given above, generate the following classes for Visual Basic .NET: imports system.xml.Serialization Namespace XMLSerializationHowTo Public Class PurchaseOrder Public shipto as usaddress Public Billto as usaddress Public Comment As String System.xml.Serialization.xmlarrayItemAttribute ("item", isnullable: = false> _ Public items () As ItemsItem Public ORDERDATE AS DATE Public OrderDatespecified As Boolean END CLASS Public Class USAddress Public Name As String Public Street As String Public city as string Public State As String Public Zip as Decimal Public Country As String END CLASS Public Class ItemsItem Public ProductName As String Public Quantity AS String Public USPRICE As Decimal Public Comment As String Public shipdate as date Public shipdatespecified as boolean Public Partnum As String END CLASS End NameSpace Please note that xsd.exe does not use classes into the namespace name. The user must manually complete this operation according to the application design. In this example, the XMLSerializationHowTo namespace is used to package the class generated by xsd.exe. To create a serialized class using the XSD.exe tool, follow these steps: After creating a serialized class, open Visual Studio .NET to create a new serialization project. To create a serialized class using the XSD.exe tool, follow these steps: New C # or Visual Basic .NET console application. To create a serialized class using the XSD.exe tool, follow these steps: insert the class file generated by Xsd.exe into your project. To do this, right-click your item in the Solution Explorer window, click Add, select Add Existing Item, and browse the previously generated class file. To create a serialized class using the XSD.exe tool, follow these steps: Make sure the item contains references to System.xml and XMLSerizationHowto namespace created in the above steps. Use the Imports statement on the XML namespace, so that there is no need to define the XMLSerializer declaration in these namespaces in the code. The imports statement must be before any other declaration. Visual Basic .NET Code Imports System.xml.SerializationImports System.io Imports XMLSERIZATIONHOWTOC # Code Using System.xml.Serialization; Using system.io; Using XMLSerializationHowTo; To create a serialized class using the XSD.exe tool, follow these steps: Create an instance of the XMLSerializer class and passed the type of object to which you want to deserialize. This example uses the previously defined PurchaseOrder type. Visual Basic .NET Code Dim serializer As XmlSerializer = New XmlSerializer (GetType (PurchaseOrder)) C # code that XmlSerializer serializer = new XmlSerializer (typeof (PurchaseOrder)); used to create a sequence of Xsd.exe tool class, follow these steps Action: To read the file, call the Deserialize method and passed to the Stream, TextReader, or XmlReader class. At this point, an order will be returned. In this example, use the following code: Visual Basic .NET code DIM Reader as textReader = New StreamReader ("Po.xml") Dim Po as purchaseorder = ctype (Serializer.Deserialize (Reader), PurchaseOrder Reader.close () C # code TextReader Reader = New StreamReader ("Po.xml"); PurchaseOrder PO = (PurchaseOrder) Serializer.DeselIalize (Reader); Reader.close (); Po.xml contains data associated with orders, which follow previously defined XSD architectures. Below is the content of this file: XML Version = '1.0' encoding = 'UTF-16'?> shipto> BillTo> item> item> items> purchaseorder> To create a serialized class using the XSD.exe tool, follow these steps: Operating the PurchaseOrder object (via the PO variable) in a general manner of the action. To create a serialized class using the XSD.exe tool, follow these steps: Save the modified data into a new XML file. To write to the file, call the Serialize method and pass to the Stream, TextReader or XMLReader class, and an instance of the order: Visual Basic .NET code DIM WRITER AS textWriter = New StreamWriter ("po2.xml") Serializer.Serialize (Writer, Po) Writer.close () C # code textWriter Writer = New StreamWriter ("PO2.xml"); Serializer.Serialize (Writer, Po); Writer.close (); To create a serialized class using the XSD.exe tool, follow these steps: Save and close the project. Reference XMLSerializer, Streamwriter and For more information on the StreamReader class, see the .NET Framework Class library documentation. For more information on serialization, see the .NET Framework Developer Guide document. For more information on how to use XML Architecture Define Tools (XSD.exe, see the .NET Framework Developer Document Or .NET Framework Tool Document. Back to top The information in this article applies to: Microsoft .Net Developments Platform (NDP) Beta 2 Recent Updated: 2001-11-2 (1.0) Keyword Kbhowto KbhowTomaster Kbxml Tslic_tslic KB301223 KBAUDDEVELOPER