C# Xml 反序列化问题
在我的项目中,遗留代码生成具有以下结构的 xml:
<Output>
<Template recordID=12>
<Employer type="String">
<Value>Google</Value>
<Value>GigaSoft inc.</Value>
</Employer>
<Designation type="String">
<Value>Google</Value>
</Designation>
<Duration type="String" />
</Template>
</Output>
我想将此 xml 反序列化为具有以下属性的对象(我使用 C#):
public class EmployerInfo
{
string[] _employerName;
string[] _designation;
string _duration;
}
我尝试使用成员周围的以下属性来反序列化上述 xml (注意:我有简化的代码。我知道我们不应该公开数据成员。此代码仅用于实验目的)
[XmlElement("Template")]
public class EmployerInfo
{
[XmlElement("Employer")]
public string[] _employerName;
[XmlElement("Designation")]
public string[] _designation;
[XmlElement("Duration")]
public string _duration;
}
为了反序列化,在主类中我写道:
XmlSerializer serial = new XmlSerializer(typeof(Output));
TextReader reader = new StreamReader(@"C:\sample_xml.xml");
EmployerInfo fooBar = (EmployerInfo)serial.Deserialize(reader);
reader.Close();
执行上述代码后,fooBar对象中的所有成员都设置为null (默认值)。我认为这是因为xml结构与类结构不匹配。
我尝试使用 xsd 命令自动生成类,但它为每个数据成员创建了单独的类。
我尝试提供诸如 XmlElement("Employer.Value") 、 XmlElement("Template.Employer.Value") 之类的元素名称,但这也不起作用。
谁能建议一些方法将此 xml 放入 EmployerInfo
类中?
提前致谢
In my project, legacy code generates xml which has following structure :
<Output>
<Template recordID=12>
<Employer type="String">
<Value>Google</Value>
<Value>GigaSoft inc.</Value>
</Employer>
<Designation type="String">
<Value>Google</Value>
</Designation>
<Duration type="String" />
</Template>
</Output>
I want to deserialize this xml into object which has following properties (I am using C#):
public class EmployerInfo
{
string[] _employerName;
string[] _designation;
string _duration;
}
I tried to deserialize above xml using following attributes around members (NOTE : I have simplified code. I know we should not make data members public. This code is just for experimental purpose)
[XmlElement("Template")]
public class EmployerInfo
{
[XmlElement("Employer")]
public string[] _employerName;
[XmlElement("Designation")]
public string[] _designation;
[XmlElement("Duration")]
public string _duration;
}
To deserialize, in main class I wrote :
XmlSerializer serial = new XmlSerializer(typeof(Output));
TextReader reader = new StreamReader(@"C:\sample_xml.xml");
EmployerInfo fooBar = (EmployerInfo)serial.Deserialize(reader);
reader.Close();
After executing above code, all the members in fooBar object are set to null (default values). I think this is because xml structure does not match with class structure.
I tried to automatically generate class using xsd command but it created seperate classes for each of the data member .
I tried to give element names like XmlElement("Employer.Value") , XmlElement("Template.Employer.Value") but this also didnt work.
Can anyone please suggest some way to fit this xml into a EmployerInfo
class ?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
另请注意,从
XmlSerializer(typeof(Output))
的反序列化方法返回的类型将是Output
记录。Try:
Note also tat the type returned from
XmlSerializer(typeof(Output))
's deserialize method is going to be aOutput
record.