XML反序列化-- 多个相同元素(每个元素包含多节点)

前端之家收集整理的这篇文章主要介绍了XML反序列化-- 多个相同元素(每个元素包含多节点)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XML格式:

<Root>
  <CustomerInfo>
    <Parameters>
      <Parameter ParaName="Customer Name" ControlType="TextBox" ParaValue="" required="1" AffectPara=""></Parameter>
      <Parameter ParaName="Region" ControlType="DropdownList" ParaValue="Asia,Europe,Other" required="1" AffectPara="Country"></Parameter>
      <Parameter ParaName="Country" ControlType="DropdownList" ParaValue="" required="1" AffectPara=""></Parameter>
      <Parameter ParaName="Shipping address" ControlType="TextBox" ParaValue="" required="1" AffectPara=""></Parameter>
      <Parameter ParaName="Attention To" ControlType="TextBox" ParaValue="" required="1" AffectPara=""></Parameter>
      <Parameter ParaName="Project / Evaluation" ControlType="TextBox" ParaValue="" required="1" AffectPara=""></Parameter>
      <Parameter ParaName="Quantity" ControlType="DropdownList" ParaValue="5,10,15,20" required="1" AffectPara=""></Parameter>
      <Parameter ParaName="Other information" ControlType="TextBox" ParaValue="" required="0" AffectPara=""></Parameter>
    </Parameters>
  </CustomerInfo>
</Root>

关于XML序列化/反序列化的工具类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Xml;
using System.Xml.Serialization;

/// <summary>
/// Xml序列化与反序列化
/// </summary>
public class XmlUtility
{
    #region 反序列化
    /// <summary>
    /// 反序列化
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="xml">XML字符串</param>
    /// <returns></returns>
    public static object Deserialize(Type type,string xml)
    {
        try
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }
        catch (Exception e)
        {
            return null;
        }
    }
    /// <summary>
    /// 反序列化
    /// </summary>
    /// <param name="type"></param>
    /// <param name="xml"></param>
    /// <returns></returns>
    public static object Deserialize(Type type,Stream stream)
    {
        XmlSerializer xmldes = new XmlSerializer(type);
        return xmldes.Deserialize(stream);
    }
    #endregion

    #region 序列化
    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="obj">对象</param>
    /// <returns></returns>
    public static string Serializer(Type type,object obj)
    {
        MemoryStream Stream = new MemoryStream();
        XmlSerializer xml = new XmlSerializer(type);
        try
        {
            //序列化对象
            xml.Serialize(Stream,obj);
        }
        catch (InvalidOperationException)
        {
            throw;
        }
        Stream.Position = 0;
        StreamReader sr = new StreamReader(Stream);
        string str = sr.ReadToEnd();

        sr.Dispose();
        Stream.Dispose();

        return str;
    }

    #endregion
}

实体类:

    public class Root
    {
        private CustomerInfo _customerInfo;
        [XmlElement("CustomerInfo")]
        public CustomerInfo CustomerInfo
        {
            get { return _customerInfo; }
            set { _customerInfo = value; }
        }
    }
    public class CustomerInfo
    {
        private Parameters _parameters;
        [XmlElement("Parameters")]
        public Parameters Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }
    }
    public class Parameters
    {
        private Parameter[] _parameter;
        [XmlElement("Parameter")]
        public Parameter[] Parameter
        {
            get { return _parameter; }
            set { _parameter = value; }
        }
    }
    public class Parameter
    {
        private string _paraName;
        [XmlAttribute("ParaName")]
        public string ParaName
        {
            get { return _paraName; }
            set { _paraName = value; }
        }
        private string _controlType;
        [XmlAttribute("ControlType")]
        public string ControlType
        {
            get { return _controlType; }
            set { _controlType = value; }
        }
        private string _paraValue;
        [XmlAttribute("ParaValue")]
        public string ParaValue
        {
            get { return _paraValue; }
            set { _paraValue = value; }
        }
        private string _required;
        [XmlAttribute("required")]
        public string required
        {
            get { return _required; }
            set { _required = value; }
        }
        private string _affectPara;
        [XmlAttribute("AffectPara")]
        public string AffectPara
        {
            get { return _affectPara; }
            set { _affectPara = value; }
        }
    }

调用反序列化方法得到Parameter的数组集合:
string path = System.AppDomain.CurrentDomain.BaseDirectory;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../test.xml");
Root root = XmlUtility.Deserialize(typeof(Root),xmlDoc.OuterXml) as Root;
Parameter[] Parameter = root.CustomerInfo.Parameters.Parameter;


调用序列化方法得到XML:

string xml = XmlUtility.Serializer(typeof(Root),root);
Console.WriteLine(xml);
Console.ReadKey();
原文链接:https://www.f2er.com/xml/296082.html

猜你在找的XML相关文章