301223 How TO: Object reading and writing (from MKBA) using XML serialization

xiaoxiao2021-03-06  76

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.

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:

alice smith

123 MAPLE Street

Mill Valley

CA

90952

robert smith

8 Oak Avenue

Old Town

PA

95819

Hurry, My Lawn is Going Wild!

Lawnmower

1

148.95

confirm this is electric literary

Baby Monitor

1

39.98

1999-05-21

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

转载请注明原文地址:https://www.9cbs.com/read-108969.html

New Post(0)