【发布时间】:2015-06-08 13:33:05
【问题描述】:
我有以下 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<TestConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Barcode>MB-B3-00</Barcode>
<TestSuites>
<Test>USB A Slave Port</Test>
<Test>USB B Host Port</Test>
</TestSuites>
</TestConfiguration>
我想把它反序列化成下面的类:
public class TestConfiguration
{
private string _barcode;
private string[] _testSuites;
private string[] _testcase;
//Product barcode
public string Barcode
{
get{return this._barcode;}
set{this._barcode = value;}
}
//Test suites
[System.Xml.Serialization.XmlArrayItemAttribute("Test", IsNullable = false)]
public string[] Testsuites
{
get{return this._testSuites;}
set{this._testSuites = value;}
}
//individual test
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Testcase
{
get{return this._testcase;}
set{this._testcase = value;}
}
}
我的反序列化代码是:
XmlSerializer serializer = new XmlSerializer(typeof(TestConfiguration));
StreamReader reader = new StreamReader(filename);
TestConfiguration _testConfig = (TestConfiguration)serializer.Deserialize(reader);
reader.Close();
但是,_testConfig 对象仅包含 Barcode 值,属性 Testcase 和 TestSuites 为空。请问有什么建议吗?
【问题讨论】:
-
您错误地使用了 XmlArrayAttribute - 请参阅此问题:stackoverflow.com/questions/15907357/…。你需要定义另一个类来做你想做的事情。
-
如果手动填充类,然后将其序列化为 XML,XML 会是什么样子?
-
您希望在
Testcase中看到什么值?