2014-05-02 23 views
0

我使用silverlight5和c#來實現我的目標。我有以下的xml文件:如何在xml中反序列化包含List的xmlelement

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> 

在反序列化我有3個類。

parameter.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; 


namespace Model.XML 
{ 
    [XmlRoot("parameter")] 
    public class parameter 
    { 
     public string name { get; set; } 
     public string label { get; set; } 

     [XmlElement("component")] 
     public component component { get; set; } 
    } 
} 

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); 

      } 
     } */ 
    } 
} 

我的問題是我不能夠看到 「1000000」 和 「5000000」 和「10 000 000「在調試時,當我試圖做Debug.WriteLine(attrib.item);在屬性類也」ccypair「顯示null做Debug.WriteLine(attrib.ccypair); (可能是因爲這個XML的結構包含兩個<attributes>..</attributes> <attributes>..</attributes>)。 如何讓他們?因爲我的下一步是顯示使用它們的GUI,所以我將需要它們的值。可以請一個人,因爲我是一個開始。

我需要做一些事情,如:

[XmlArray("component"), XmlArrayItem("attributes", typeof(attributes))] 
     public List<attributes> component 
     { 
      get { return (_attributes); } 
      set { _attributes = value; } 
     } 
     private List<attributes> _attributes = new List<attributes>(); 

因爲這個 「項目」,其中包含 「1 000 000」, 「5000000」 等都是 「屬性」 裏面?請給我解釋一下我是初學者

+0

「1 000 000」和「5 000 000」和「10 000 000」是字符串值不是整數。 'attributes.item'被聲明爲'int'。 – rageit

+0

@rageit要我糾正它。它會如何工作,即使這樣做,爲什麼它在調試時顯示「ccypair」爲空值。 – Sss

+0

@rageit我需要做這樣的事情嗎? 「[XmlArray(」 組件 「),XmlArrayItem(」 屬性 「的typeof(屬性))] 公共列表部件 { 得到{(_attributes);} 集合{_attributes =值;} }」 什麼你建議? – Sss

回答

2

使用下面的代碼,你可以序列化和反序列化對象,你甚至可以存儲在XML

SerializeObject(objDividendList, filename); 

objDividendList = DeSerializeObject>(文件名);

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; 
    } 
+0

感謝您的回答,但您是否閱讀過我的問題?我已經seserialized,讓我的xml字符串「xmlstring」和問題是當我調試「Debug.WriteLine(attrib.item);」那麼我不能看到「1 000 000」,「500 000」等。爲什麼?而在做「Debug.WriteLine(attrib.type);」顯示「列表」(意味着爲它工作)。如何查看我想查看的內容?另外當我嘗試看到「Debug.WriteLine(attrib.ccypair);」它顯示爲「ccypair」爲空。 – Sss

+0

請再次看到問題。我已經反序列化了。我需要這些值,因爲我必須創建對應於此列表值「1 000 000」的GUI等 – Sss

+0

我需要做這樣的事情嗎? 「[XmlArray(」component「),XmlArrayItem(」attributes「,typeof(attributes))] public List component {get {return(_attributes);} set {_attributes = value;}}」您提議什麼? – Sss