我有一个简单的XML文件,其中包含一个对象数组:
<?xml version="1.0" encoding="UTF-8"?>
<complexes>
<complex>
<name>NAME1</name>
</complex>
</complexes>
所以我做了一个类结构来适应它:
public complex[] complexes;
public class complex
{
public string name;
}
我用标准的c#工具做解析:
XmlSerializer reader = new XmlSerializer(typeof(complex[]));
StreamReader file = new StreamReader("test.xml");
complexes = (complex[])reader.Deserialize(file);
由于某些原因,我在最后一行得到一个异常:
System.InvalidOperationException: 'There is an error in XML document (2, 2).'
InvalidOperationException: <complexes xmlns=''> was not expected.
有什么问题吗?
以下作品:
using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp2
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(complexes));
XmlReader reader = XmlReader.Create(FILENAME);
complexes complexes = (complexes)serializer.Deserialize(reader);
}
}
public class complexes
{
[XmlElement("complex")]
public complex[] complex { get; set; }
}
public class complex
{
public string name { get; set; }
}
}