????? C# ?? ????? ?? XML ?? ???? ???????? ?? serialize ???? ????

???? ?????? ???? ??????
???? ID: 815813 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ???? ????? ???? ?? ?? ????? C# ?? ????? ?? XML ?? ???? ???????? ?? serialize ???? ????? ?? ???? ?? ???????? ?? ?????? persisting ?? ??? ?????? ??? ?? ???? ?? ???? ???????? ?? ???? ?? ???????? ?? XML de-serializing ?????? ???????? ?? ??? ?????? ???



??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ?????? ?? ?????? ??? ?? ???? ???:
  • Microsoft Visual Studio
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • General familiarity with XML
  • General familiarity with Visual C#

XML Serialization

Serialization is the process of taking the state of an object and persisting it in some fashion. The Microsoft .NET Framework includes powerful objects that can serialize any object to XML. TheSystem.Xml.Serializationnamespace provides this capability.

Follow these steps to create a console application that creates an object, and then serializes its state to XML:
  1. In Visual C#, create a new Console Application project.
  2. ????? ????????????????? ??,Add Classto add a new class to the project.
  3. ?????Add New Itemdialog box, change the name of the class toclsPerson.
  4. ????? ????,add. A new class is created.

    ???:In Visual Studio .NET 2003, click?????.
  5. Add the following code after the Public ClassclsPersonstatement
     public   string FirstName;
     public   string MI;
     public   string LastName;
    
  6. Switch to the code window for Program.cs in Visual Studio or for Class1.cs in Visual Studio .NET 2003.
  7. In the void?????method, declare and create an instance of theclsPersonclass:
    clsPerson p = new clsPerson();
  8. Set the properties of theclsPersonobject:
    p.FirstName = "Jeff";
    p.MI = "A";
    p.LastName = "Price";
  9. TheXml.Serializationnamespace contains anXmlSerializerclass that serializes an object to XML. When you create an instance ofXmlSerializer, you pass the type of the class that you want to serialize into its constructor:
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
  10. TheSerializemethod is used to serialize an object to XML. Serialize is overloaded and can send output to aTextWriter,Stream??,XMLWriter???????? ??? In this example, you send the output to the console:
    x.Serialize(Console.Out,p);
    Console.WriteLine();
    Console.ReadLine();

Complete Code Listing

using System;

public class clsPerson
{
  public  string FirstName;
  public  string MI;
  public  string LastName;
}

class class1
{ 
   static void Main(string[] args)
   {
      clsPerson p=new clsPerson();
      p.FirstName = "Jeff";
      p.MI = "A";
      p.LastName = "Price";
      System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
      x.Serialize(Console.Out, p);
      Console.WriteLine();
      Console.ReadLine();
   }
}    

Verification

To verify that your project works, press CTRL+F5 to run the project. AclsPersonobject is created and populated with the values that you entered. This state is serialized to XML. The console window shows the following:
<?xml version="1.0" encoding="IBM437"?>
 <clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3
 .org/2001/XMLSchema">
<FirstName>Jeff</FirstName>
 <MI>A</MI>
<LastName>Price</LastName> </clsPerson>

?????? ??????

TheXml.Serialization.XmlSerializer???????? ?? ???? shallow serialization ???? ??? ??? ?? ?? ??? ???????? ?? ?????? ?????????? ?? ???? ?? serialize ???? ????? ???, ?? ?? ??? serialization ?? ????? ???? ??????

??????

???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????::
XML Serialization introducing
(VS.80) http://MSDN.Microsoft.com/en-us/library/182eeyhh .aspx

???

???? ID: 815813 - ????? ???????: 23 ??????? 2011 - ??????: 3.0
???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
??????: 
kbsweptvs2008 kbnamespace kbxml kbhowtomaster kbmt KB815813 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:815813

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com