我使用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」 等都是 「屬性」 裏面?請給我解釋一下我是初學者
「1 000 000」和「5 000 000」和「10 000 000」是字符串值不是整數。 'attributes.item'被聲明爲'int'。 – rageit
@rageit要我糾正它。它會如何工作,即使這樣做,爲什麼它在調試時顯示「ccypair」爲空值。 – Sss
@rageit我需要做這樣的事情嗎? 「[XmlArray(」 組件 「),XmlArrayItem(」 屬性 「的typeof(屬性))] 公共列表部件 { 得到{(_attributes);} 集合{_attributes =值;} }」 什麼你建議? –
Sss