XML Serialization in C#

前端 未结 7 1524
别那么骄傲
别那么骄傲 2021-02-08 17:37

Where Can I find good tutorial about XMl serialization to the object? Thanks.

相关标签:
7条回答
  • 2021-02-08 18:13

    MSDN has a decent article about it: http://msdn.microsoft.com/en-us/library/ms733901.aspx

    And this one's a bit more straightforward: http://www.dotnetjohn.com/articles.aspx?articleid=173

    0 讨论(0)
  • 2021-02-08 18:14

    We use Serialization to write the data in Binary Format and IN XML format. for binary format we use BibnaryFormatSerialization and for XML format we use SoapFormatSerialization.

    0 讨论(0)
  • 2021-02-08 18:21

    There's a basic tutorial on Microsoft's support pages and their code example is only a few lines long:

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

    Basically you don't have to anything other than call the built in functions that do all the hard work for you.

    0 讨论(0)
  • 2021-02-08 18:28

    Its really pretty simple, there are only three main steps.

    1. You need to mark your classes with the [Serializable] attribute.
    2. Write Serialization code
    3. Write Deserialization code

    Serialization:

    var x = new XmlSerializer(typeof(YourClass));
    var fs = new FileStream(@"C:\YourFile.xml"), FileMode.OpenOrCreate);
    x.Serialize(fs, yourInstance);
    fs.Close();
    

    Deserialization:

    var x = new XmlSerializer(typeof(YourClass));
    var fs = new FileStream(@"C:\YourFile.xml"), FileMode.Open);
    var fromFile = x.Deserialize(fs) as YourClass;
    fs.Close();
    
    0 讨论(0)
  • 2021-02-08 18:28

    XML Serialization in the .NET Framework

    How to serialize an object to XML by using Visual C#

    0 讨论(0)
  • 2021-02-08 18:30

    Here's a good start microsoft

    Also look into Xml Schema and generating classes automatically with xsd.exe the sooner you get used to this the better, it can save you a lot of effort working with XML. Also looking at the generated c# files gives you some clues on how to use attributes to manipulate the way classes are serilized by the XmlSerializer

    0 讨论(0)
提交回复
热议问题