C#/.NET应用程序编程开发中将一个对象(object)序列化成xml文档的方法有哪些?

C#开发 作者: Rector 9 次阅读 · 读完约需 1 分钟 收藏本文

郑重申明:本文未经许可,禁止任何形式转载

问题描述

C#/.NET应用程序编程开发中,现有一个对象object,要求将这个对象序列化成功xml文档,有哪些方法呢?

方案一

使用命名空间System.Xml.SerializationXmlSerializer类来将对象序列化成xml,如下:

public static class SerializeExtension
{
    public static string Serialize<T>(this T value)
    {
        if (value == null)
        {
            return string.Empty;
        }
        try
        {
            var xmlserializer = new XmlSerializer(typeof(T));
            var stringWriter = new StringWriter();
            using (var writer = XmlWriter.Create(stringWriter))
            {
                xmlserializer.Serialize(writer, value);
                return stringWriter.ToString();
            }
        }
        catch (Exception ex)
        {
            throw new Exception("An error occurred", ex);
        }
    }
}

完整示例:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApp2
{
    class Program
    {
        public static void Main()
        {
            var customer = new Customer
            {
                Id = 1,
                DisplayName ="Rector"
            };
            var customerXml = customer.Serialize();
            Console.WriteLine(customerXml);
            Console.ReadLine();
        }
    }

    public class Customer
    {
        public int Id { get; set; }
        public string DisplayName { get; set; }
    }

    public static class SerializeExtension
    {
        public static string Serialize<T>(this T value)
        {
            if (value == null)
            {
                return string.Empty;
            }
            try
            {
                var xmlserializer = new XmlSerializer(typeof(T));
                var stringWriter = new StringWriter();
                using (var writer = XmlWriter.Create(stringWriter))
                {
                    xmlserializer.Serialize(writer, value);
                    return stringWriter.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred", ex);
            }
        }
    }
}

输出结果:

<?xml version="1.0" encoding="UTF-8"?>
<Customer xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Id>1</Id>
   <DisplayName>Rector</DisplayName>
</Customer>

方案二

使用XmlDocument序列化对象成xml文档,如下:

public string SerializeObject(object obj)
{
    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
        serializer.Serialize(ms, obj);
        ms.Position = 0;
        xmlDoc.Load(ms);
        return xmlDoc.InnerXml;
    }
}

阅读了该文章的人还浏览了...

本文永久链接码友网 » C#/.NET应用程序编程开发中将一个对象(object)序列化成xml文档的方法有哪些?

发布于: 2019-07-11 14:01:54
分享扩散:

发表评论

登录用户才能发表评论, 请 登 录 或者 注册