c#

C# XML序列化有哪些方法

小樊
83
2024-09-14 20:53:40
栏目: 编程语言

在C#中,可以使用多种方法进行XML序列化

  1. 使用XmlSerializer类:这是最常用的方法,通过创建XmlSerializer对象并调用其SerializeDeserialize方法来实现对象的序列化和反序列化。
using System.IO;
using System.Xml.Serialization;

public class Program
{
    public static void Main()
    {
        // 创建一个要序列化的对象
        Person person = new Person { Name = "John", Age = 30 };

        // 创建一个XmlSerializer对象
        XmlSerializer serializer = new XmlSerializer(typeof(Person));

        // 将对象序列化为XML字符串
        using (StringWriter writer = new StringWriter())
        {
            serializer.Serialize(writer, person);
            string xmlString = writer.ToString();
            Console.WriteLine("Serialized XML: " + xmlString);
        }

        // 从XML字符串反序列化为对象
        using (StringReader reader = new StringReader(xmlString))
        {
            Person deserializedPerson = (Person)serializer.Deserialize(reader);
            Console.WriteLine("Deserialized object: Name = {0}, Age = {1}", deserializedPerson.Name, deserializedPerson.Age);
        }
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
  1. 使用DataContractSerializer类:这是另一种用于序列化和反序列化对象的方法,主要用于WCF服务。与XmlSerializer相比,DataContractSerializer提供了更好的性能和更简洁的XML输出。
using System.IO;
using System.Runtime.Serialization;

public class Program
{
    public static void Main()
    {
        // 创建一个要序列化的对象
        Person person = new Person { Name = "John", Age = 30 };

        // 创建一个DataContractSerializer对象
        DataContractSerializer serializer = new DataContractSerializer(typeof(Person));

        // 将对象序列化为XML字符串
        using (MemoryStream stream = new MemoryStream())
        {
            serializer.WriteObject(stream, person);
            string xmlString = Encoding.UTF8.GetString(stream.ToArray());
            Console.WriteLine("Serialized XML: " + xmlString);
        }

        // 从XML字符串反序列化为对象
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
        {
            Person deserializedPerson = (Person)serializer.ReadObject(stream);
            Console.WriteLine("Deserialized object: Name = {0}, Age = {1}", deserializedPerson.Name, deserializedPerson.Age);
        }
    }
}

[DataContract]
public class Person
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Age { get; set; }
}
  1. 使用XmlDocumentXDocument类:这些类提供了对XML文档的直接操作,允许您手动构建和解析XML结构。这种方法比使用序列化器更复杂,但提供了更大的控制和灵活性。
using System.Xml;
using System.Xml.Linq;

public class Program
{
    public static void Main()
    {
        // 创建一个要序列化的对象
        Person person = new Person { Name = "John", Age = 30 };

        // 使用XDocument手动构建XML
        XDocument doc = new XDocument(
            new XElement("Person",
                new XElement("Name", person.Name),
                new XElement("Age", person.Age)
            )
        );

        // 将XDocument转换为字符串
        string xmlString = doc.ToString();
        Console.WriteLine("Serialized XML: " + xmlString);

        // 从XML字符串反序列化为对象
        XDocument deserializedDoc = XDocument.Parse(xmlString);
        Person deserializedPerson = new Person
        {
            Name = deserializedDoc.Root.Element("Name").Value,
            Age = int.Parse(deserializedDoc.Root.Element("Age").Value)
        };
        Console.WriteLine("Deserialized object: Name = {0}, Age = {1}", deserializedPerson.Name, deserializedPerson.Age);
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

这些方法都可以用于在C#中进行XML序列化。选择哪种方法取决于您的需求和项目的特定要求。

0
看了该问题的人还看了