把类序列化为xml字符串

 

把类序列化为xml字符串

using System; using System.Text; using System.XML; using System.IO; 

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());

            StringBuilder sb 
= new StringBuilder();
            StringWriter sw 
= new StringWriter(sb);
           
            x.Serialize(sw, p);

            Console.Write(sb.ToString());
            Console.ReadLine();
        }
    }

你可能感兴趣的:(xml)