我正在尝试序列化以下XML。但是“键”部分并未填充,并且在序列化对象中为空。
<?xml version="1.0"?>
<Golden>
<SecType>
<ID>New</ID>
<Count>1</Count>
</SecType>
<Request>
<action>New</action>
<Keys>
<Key>
<ReferenceType>AAA</ReferenceType>
<ReferenceValue>1111</ReferenceValue>
<Description></Description>
</Key>
<Key>
<ReferenceType>BBBB</ReferenceType>
<ReferenceValue>22222</ReferenceValue>
<Description></Description>
</Key>
</Keys>
</Request></Golden>
我正在尝试创建一个自定义类对象以供进一步使用。请在下面查看我的代码。
[Serializable]
[XmlRootAttribute("Golden")]
public class Process
{
[XmlElementAttribute("SecType")]
public SecType secType { get; set; }
[XmlElementAttribute("Request")]
public Request request { get; set; }
}
[Serializable]
[XmlRootAttribute("SecType")]
public class SecType
{
[XmlElementAttribute("ID")]
public string ID { get; set; }
[XmlElementAttribute("Count")]
public int Count { get; set; }
}
[Serializable]
[XmlRootAttribute("Request")]
public class Request
{
[XmlElementAttribute("action")]
public string action { get; set; }
[XmlElementAttribute("Keys")]
public Keys keys { get; set; }
}
[Serializable()]
[XmlRootAttribute("Keys")]
public class Keys
{
[XmlArray("Keys")]
[XmlArrayItem("Key",typeof(Key))]
public Key[] key { get; set; }
}
[Serializable]
[XmlRootAttribute("Key")]
public class Key
{
[XmlElementAttribute("ReferenceType")]
public string ReferenceType { get; set; }
[XmlElementAttribute("ReferenceValue")]
public string ReferenceValue { get; set; }
[XmlElementAttribute("Description")]
public string Description { get; set; }
}
string sPath = @"C:\Test\ConsoleApp1\test.xml";
Process proc = new Process();
XmlSerializer serializer = new XmlSerializer(typeof(Process));
StreamReader reader = new StreamReader(sPath);
proc = (Process)serializer.Deserialize(reader);
reader.Close();
我主要指this。但这在我的实施中不起作用。感谢您的帮助