C# 如何反序列化xml中包含列表的xmlelement

C# 如何反序列化xml中包含列表的xmlelement,c#,xml,deserialization,xml-deserialization,C#,Xml,Deserialization,Xml Deserialization,我正在使用silverlight5和c#来实现我的目标。我有以下xml文件: string xmlstring = <?xml version="1.0" encoding="utf-8" ?> <parameter> <name>bands_amounts</name> <label>Bands Amounts</label> <unit></unit> <component

我正在使用silverlight5和c#来实现我的目标。我有以下xml文件:

string xmlstring = 
<?xml version="1.0" encoding="utf-8" ?>
<parameter>
  <name>bands_amounts</name>
  <label>Bands Amounts</label>
  <unit></unit>
  <component>
    <type>List</type>
    <attributes>
      <type>Integer</type>
      <displayed>4</displayed>
      <add_remove>yes</add_remove> 
      <item>1 000 000</item>
      <item>5 000 000</item>
      <item>10 000 000</item>
      <item>20 000 000</item>
    </attributes>
    <attributes>
      <ccypair>XAUUSD</ccypair>
      <item>100</item>
      <item>500</item>
      <item>1000</item>
    </attributes>
  </component >
</parameter>


因为这个包含“1 000 000”、“5 000 000”等的“项目”在“属性”内?请解释我是初学者

使用下面的代码,您可以序列化和反序列化您的对象,即使您可以存储xml

SerializeObject(objDividendList, filename);
objDividendList=反序列化对象>(文件名)

public void serialized对象(T serializableObject,字符串文件名)
{
如果(serializableObject==null){return;}
尝试
{
XmlDocument XmlDocument=新的XmlDocument();
XmlSerializer serializer=新的XmlSerializer(serializableObject.GetType());
使用(MemoryStream stream=new MemoryStream())
{
serializer.Serialize(流,serializableObject);
流位置=0;
加载(流);
xmlDocument.Save(文件名);
stream.Close();
}
}
捕获(例外情况除外)
{
//此处记录异常
log.Error(“序列化对象”,ex);
}
}
公共T反序列化对象(字符串文件名)
{
if(string.IsNullOrEmpty(fileName)){返回默认值(T);}
T objectOut=默认值(T);
尝试
{
string attributeXml=string.Empty;
XmlDocument XmlDocument=新的XmlDocument();
加载(文件名);
字符串xmlString=xmlDocument.OuterXml;
使用(StringReader读取=新的StringReader(xmlString))
{
类型outType=类型of(T);
XmlSerializer serializer=新的XmlSerializer(outType);
使用(XmlReader=新的XmlTextReader(读取))
{
objectOut=(T)序列化程序。反序列化(读取器);
reader.Close();
}
read.Close();
}
}
捕获(例外情况除外)
{
//此处记录异常
log.Error(“反序列化对象”,ex);
}
退出;
}

谢谢你的回答,但你读过我的问题了吗?我已经序列化了我的xml并将其放在字符串“xmlstring”中,问题是当我调试“Debug.WriteLine(attrib.item);”时,我无法看到“1000 000”、“5000 000”等。为什么?而在执行“Debug.WriteLine(attrib.type);”时,会显示“List”(表示为其工作)。如何查看我想要查看的内容?另外,当我尝试查看“Debug.WriteLine(attrib.ccypair);”时,“ccypair”显示为null。请再次查看问题。我已经应得了。我需要这些值,因为在我必须创建对应于这个列表值“1000000”等的GUI之后,我需要这样做吗?“[XmlArray(“组件”)、XmlArrayItem(“属性”、typeof(属性))]公共列表组件{get{return(_attributes)}set{u attributes=value;}}}”你有什么建议?“10000000”、“5000000”和“10000000”是字符串值而不是整数<代码>属性。项目声明为
int
@rageit thaks,我正在更正它。它甚至可以这样做吗?为什么它在调试时将“ccypair”显示为空值。@rageit我需要这样做吗?“[XmlArray(“组件”)、XmlArrayItem(“属性”、typeof(属性))]公共列表组件{get{return(_attributes)}set{u attributes=value;}}”你有什么建议?如果你想要像“10000000”、“5000000”和“10000000”这样的字符串为了与整数值100、500和1000一起考虑,我建议将
item
类型从
List
更改为
List
attribute.cs:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;


namespace Model.XML
{
    public class attributes
    {

        public string type { get; set; }    
        public string displayed { get; set; }
        public string add_remove { get; set; }
        public string ccypair { get; set; }
        public List<int> item { get; set; }

         public static void Main()
        {          
            string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?> <parameter> <name>bands_amounts</name>   <label>Bands Amounts</label>   <unit>54</unit>  <component>    <type>List</type>    <attributes>      <type>Integer</type>     <displayed>4</displayed>    <add_remove>yes</add_remove>       <item>1 000 000</item>       <item>5 000 000</item>       <item>10 000 000</item>       <item>20 000 000</item>   </attributes>    <attributes>   <ccypair>XAUUSD</ccypair> <item>100</item> <item>500</item> <item>1000</item> </attributes>  </component > </parameter>";
            XmlSerializer serializer = new XmlSerializer(typeof(parameter));
            XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
            var Object1 = (parameter)serializer.Deserialize(reader);
            foreach (var attrib in Object1.component.attribut)
            {
                Debug.WriteLine(attrib.type);
                Debug.WriteLine(attrib.item);

            }
        }  
    }
}
component.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;

namespace Model.XML
{
    public class component
    {

        [XmlElement("attributes")]
        public List<attributes> attribut { get; set; }

    /*    public static void Main()
        {
            string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?> <parameter> <name>bands_amounts</name>   <label>Bands Amounts</label>   <unit>54</unit>  <component>    <type>List</type>    <attributes>      <type>Integer</type>     <displayed>4</displayed>    <add_remove>yes</add_remove>       <item>1 000 000</item>       <item>5 000 000</item>       <item>10 000 000</item>       <item>20 000 000</item>   </attributes>    <attributes>   <ccypair>XAUUSD</ccypair> <item>100</item> <item>500</item> <item>1000</item> </attributes>  </component > </parameter>";

            XmlSerializer deserializer = new XmlSerializer(typeof(parameter));          
            XmlReader reader = XmlReader.Create(new StringReader(xmlstring));

            var Object1 = (parameter)deserializer.Deserialize(reader);
            foreach (var attrib in Object1.component.attribut)
            {
                Debug.WriteLine(attrib.item);
              //  Debug.WriteLine(Object1.name);

            }
        }   */
    }
}
 [XmlArray("component"), XmlArrayItem("attributes", typeof(attributes))]
        public List<attributes> component
        {
            get { return (_attributes); }
            set { _attributes = value; }
        }
        private List<attributes> _attributes = new List<attributes>();
SerializeObject(objDividendList, filename);
public void SerializeObject<T>(T serializableObject, string fileName)
    {
        if (serializableObject == null) { return; }

        try
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, serializableObject);
                stream.Position = 0;
                xmlDocument.Load(stream);
                xmlDocument.Save(fileName);
                stream.Close();
            }
        }
        catch (Exception ex)
        {
            //Log exception here
            log.Error("SerializeObject ", ex);

        }
    }

    public T DeSerializeObject<T>(string fileName)
    {
        if (string.IsNullOrEmpty(fileName)) { return default(T); }

        T objectOut = default(T);

        try
        {
            string attributeXml = string.Empty;

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            string xmlString = xmlDocument.OuterXml;

            using (StringReader read = new StringReader(xmlString))
            {
                Type outType = typeof(T);

                XmlSerializer serializer = new XmlSerializer(outType);
                using (XmlReader reader = new XmlTextReader(read))
                {
                    objectOut = (T)serializer.Deserialize(reader);
                    reader.Close();
                }

                read.Close();
            }
        }
        catch (Exception ex)
        {
            //Log exception here
            log.Error("DeSerializeObject ", ex);

        }

        return objectOut;
    }