Serialize an object to XML

后端 未结 17 1898
渐次进展
渐次进展 2020-11-22 04:41

I have a C# class that I have inherited. I have successfully \"built\" the object. But I need to serialize the object to XML. Is there an easy way to do it?

It looks

17条回答
  •  感情败类
    2020-11-22 05:26

    Here's a basic code that will help serializing the C# objects into xml:

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

提交回复
热议问题