C#中Xml与对象Model的相互转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
 
namespace OAServiceInvoke
{
    public class XMLSerialize
    {
        public static string XmlSerialize<T>(T obj)
        {
            using (StringWriter sw = new StringWriter())
            {
                Type t = obj.GetType();
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(sw, obj);
                sw.Close();
                return sw.ToString();
            }
        }
 
        public static T DESerializer<T>(string strXML) where T:class
        {
            try
            {
                using (StringReader sr = new StringReader(strXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return serializer.Deserialize(sr) as T;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}

例子:

<?xml version="1.0" encoding="utf-8" ?>
<envelope>
    <header>
        <version port="1111" host="www.baidu.com">successfuly</version>
        <timestamp>20211216081218</timestamp>
    </header>
    <response type="cities" product="hotel">
        <cities>
            <city>
                <code value="zg" />
                <city_tax value="true" />
                <names>
                    <name language="fr" value="ABANO TERME - PADOUE" />
                    <name language="en" value="ABANO TERME - PADUE" />
                    <name language="nl" value="ABANO TERME - PADUE" />
                </names>
            </city>
            <city>
                <code value="hk" />
                <city_tax value="false" />
                <names>
                    <name language="fr" value="ABBADIA SAN SALVATORE - SIENNE" />
                    <name language="en" value="ABBADIA SAN SALVATORE - SIENA" />
                    <name language="nl" value="ABBADIA SAN SALVATORE - SIENA" />
                </names>
            </city>
        </cities>
    </response>
</envelope>

对象:

namespace Practices.Models
{
    /// 
    /// Envelope
    /// 
    [XmlType(TypeName = "envelope")]
    public class CityDataModel
    {
        /// 
        /// header
        /// 
        [XmlElement("header")]
        public Header header { get; set; }
        
        /// 
        /// response
        /// 
        [XmlElement("response")]
        public Response response { get; set; }
    }

    /// 
    /// Header
    /// 
    [XmlType(TypeName = "header")]
    public class Header
    {
        /// 
        /// version
        /// 
        [XmlElement("version")]
        public Version version { get; set; }

        /// 
        /// timestamp
        /// 
        [XmlElement("timestamp")]
        public string timestamp { get; set; }
    }

    /// 
    /// Version
    /// 
    public class Version
    {
        /// 
        /// port
        /// 
        [XmlAttribute("port")]
        public string port { get; set; }

        /// 
        /// host
        /// 
        [XmlAttribute("host")]
        public string host { get; set; }

        /// 
        /// value:XmlTextAttribute指示该属性作为XML文本处理
        /// 
        [XmlTextAttribute()]
        public string value { get; set; }

    }

    /// 
    /// Response
    /// 
    [XmlType(TypeName = "response")]
    public class Response
    {
        /// 
        /// type
        /// 
        [XmlAttribute]
        public string type { get; set; }

        /// 
        /// product
        /// 
        [XmlAttribute]
        public string product { get; set; }

        /// 
        /// cities
        /// 
        [XmlArray("cities")]
        public List<City> cities { get; set; }

    }
 
    /// 
    /// class: City
    /// 
    [XmlType(TypeName = "city")]
    public class City
    {
        /// 
        /// code
        /// 
        [XmlElement("code")]
        public Code code { get; set; }

        /// 
        /// city_tax
        /// 
        [XmlElement("city_tax")]
        public City_tax city_tax { get; set; }

        /// 
        /// names
        /// 
        [XmlArray("names")]
        public List<Name> names { get; set; }
    }
 
    /// 
    /// class: Code
    /// 
    [XmlType(TypeName = "code")]
    public class Code
    {
        /// 
        /// 
        /// 
        [XmlAttribute("value")]
        public string value { get; set; }
    }
 
    /// 
    /// class: City_tax
    /// 
    [XmlType(TypeName = "city_tax")]
    public class City_tax
    {
        /// 
        /// 
        /// 
        [XmlAttribute("value")]
        public string value { get; set; }
    }
 
    /// 
    /// class: Name
    /// 
    [XmlType(TypeName = "name")]
    public class Name
    {
        /// 
        /// 
        /// 
        [XmlAttribute("language")]
        public string language { get; set; }
        
        /// 
        /// 
        /// 
        [XmlAttribute("value")]
        public string value { get; set; }
    }
}

可以使用Visual Studio自带的Xml对象模型类

1.首先Ctrl+C复制你需要生成的Xml文档内容

2.找到编辑=》选择性粘贴=》将Xml粘贴为类,这样就可以了

XmlType(TypeName=“”)为<>的开始Name

XmlAttribute为内部关键字

XmlText()是获取<>123123的Value

你可能感兴趣的:(C#,c#,xml)