我想从XML中将具有相同名称的多个元素加载到使用C#中的反序列化的类中。我的示例中的所有内容都可以正常加载,但数组元素(Element3)未被填充。使用C#中的序列化将多个XML元素加载到对象中
代码:
class Program
{
static void Main(string[] args)
{
FileStream file = new FileStream("service.xml", FileMode.Open);
if (file != null)
{
XmlSerializer serializer = new XmlSerializer(typeof(Service));
Service service = (Service)serializer.Deserialize(file);
}
}
}
public class Service
{
public bool Element1;
public string Element2;
public string[] Element3;
}
XML:
<Service>
<Element1>true</Element1>
<Element2>Text 1</Element2>
<Element3>Text 2</Element3>
<Element3>Text 3</Element3>
</Service>
仅供参考,'file'永远不会为空。 – 2009-09-04 15:18:37