我试图将存储在.resx文件中的一些XML数据用作单元测试的测试数据。
我有这个代码,它接受测试数据,将其转换为流,并尝试将其反序列化为学生集合,我可以使用我的模拟dbset。
var stream = TestData.Students.ToStream();
var reader = new StreamReader(stream);
var serializer = new XmlSerializer(typeof(Collection<Student>));
_students = (Collection<Student>)serializer.Deserialize(reader);
学生班
public class Student
{
public int Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public char MiddleInitial { get; set; }
public DateTime? EnrollmentDate { get; set; }
[XmlIgnore]
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
在最后一行失败,出现此异常:
{"<Students xmlns=''> was not expected."}
我尝试将Serializable属性添加到Student类,尝试向其添加XMLRoot属性,也在Google上搜索了一段时间,但无法找到处理资源文件的任何内容。< / p>
另外,是否有更容易/更好的方法来获取测试数据?
修改的 如果它有助于我关注this文章。
编辑2 因此深入到内部异常的堆栈跟踪中,我找到了这一行:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCollection1.Read4_ArrayOfStudent()
所以我将XML的根元素从Students更改为ArrayOfStudent,瞧,它正确反序列化。
这是资源文件中的XML数据。
<Students>
<Student>
<Id>1</Id>
<LastName>Alexander</LastName>
<FirstName>Carson</FirstName>
<EnrollmentDate>2010-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>2</Id>
<LastName>Alonso</LastName>
<FirstName>Meredith</FirstName>
<EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>3</Id>
<LastName>Anand</LastName>
<FirstName>Arturo</FirstName>
<EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>4</Id>
<LastName>Barzdukas</LastName>
<FirstName>Gytis</FirstName>
<EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>5</Id>
<LastName>Li</LastName>
<FirstName>Yan</FirstName>
<EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>6</Id>
<LastName>Justice</LastName>
<FirstName>Peggy</FirstName>
<EnrollmentDate>2011-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>7</Id>
<LastName>Norman</LastName>
<FirstName>Laura</FirstName>
<EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>8</Id>
<LastName>Olivetto</LastName>
<FirstName>Nino</FirstName>
<EnrollmentDate>2005-08-11T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>9</Id>
<LastName>Alexander</LastName>
<FirstName>Carson</FirstName>
<EnrollmentDate>2010-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>10</Id>
<LastName>Alonso</LastName>
<FirstName>Meredith</FirstName>
<EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>11</Id>
<LastName>Anand</LastName>
<FirstName>Arturo</FirstName>
<EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>12</Id>
<LastName>Barzdukas</LastName>
<FirstName>Gytis</FirstName>
<EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>13</Id>
<LastName>Li</LastName>
<FirstName>Yan</FirstName>
<EnrollmentDate>2012-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>14</Id>
<LastName>Justice</LastName>
<FirstName>Peggy</FirstName>
<EnrollmentDate>2011-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>15</Id>
<LastName>Norman</LastName>
<FirstName>Laura</FirstName>
<EnrollmentDate>2013-09-01T00:00:00</EnrollmentDate>
</Student>
<Student>
<Id>16</Id>
<LastName>Olivetto</LastName>
<FirstName>Nino</FirstName>
<EnrollmentDate>2005-08-11T00:00:00</EnrollmentDate>
</Student>
</Students>
答案 0 :(得分:0)
您可以使用此代码从xml文件中获取数据
using (FileStream sr = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
XmlSerializer serializer = new XmlSerializer(typeof(Students));
return serializer.Deserialize(sr) as Students;
}
对于 Xmlns ,您无法使用属性来添加应添加的名称空间
XmlSerializerNamespaces np = new XmlSerializerNamespaces();
然后将新命名空间添加到 np ,添加此 np 以序列化或反序列化为参数
答案 1 :(得分:0)
尝试此代码(为部分xml添加根元素)
var xmlString = File.ReadAllText(@"C:\YourDirectory\Students.xml");
var rootAttribute = new XmlRootAttribute { ElementName = "Students", IsNullable = true };
XmlSerializer serializer = new XmlSerializer(typeof(Student[]), rootAttribute);
StringReader reader = new StringReader(xmlString);
Student[] students = (Student[])serializer.Deserialize(reader);
我只是在这里阅读文件中的全文,你可以使用一个流,赢了。
答案 2 :(得分:0)
在反序列化时,序列化程序正在寻找ArrayOf {ClassName}的根元素,因此将根元素Students更改为ArrayOfStudent就可以了。