[英]XML Serialization Not populating the array
I am trying to serialize the below XML.我正在尝试序列化下面的 XML。 But the section "Keys" are not getting populated and its coming as null in the serialized object.
但是“密钥”部分没有被填充,它在序列化的 object 中以 null 的形式出现。
<?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>
I am trying to create a custom class object for further use.我正在尝试创建一个自定义 class object 以供进一步使用。 Please see my code below.
请在下面查看我的代码。
[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();
I mainly referred this .我主要提到这个。 But it's not working in my implementation.Thanks for you help
但这在我的实施中不起作用。感谢您的帮助
I just fixed the Key Element:我刚刚修复了关键元素:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication142
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Process));
Process proc = (Process)serializer.Deserialize(reader);
}
}
[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; }
[XmlArray("Keys")]
[XmlArrayItem("Key")]
public Key[] keys { 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; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.