序列化XML问题 [英] Serializing XML problem

查看:69
本文介绍了序列化XML问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个类来序列化以下XML:


Hi, I need to write a class to serialize the following XML:

<xml>
   <body>
<Team type="request" selection="whatever"> <Test1>
</Test1> <Test2> </Test2> </Team> <Team type="input"> <Test3> </Test3> <Test4> </Test4> <Test5>
</Test5>
</Team>
</body> </xml>

我是新手到XML序列化,这就是我将序列化的方式,例如顶级团队类:

[Serializable()]
[XmlRoot(ElementName =" Team")]
公共类团队
{
私人string _test1;
private test2 _test2;

[XmlElement(ElementName =" Test1")]
公共字符串Test1
{
get {return _test1; } set {_test1 = value; } <}

[XmlElement(ElementName =" Test2")]
公共string Test2
{
get {return _test2; } set {_test2 = value; }}}}}

公共类Test2
{{br / > private string _testing;

[XmlAttribute(AttributeName =" Testing")]
public string测试
{
get {return _testing; } set {_testing = value;有问题请告诉我是否有更好的方法,以及如何序列化具有不同包含标签的Team类取决于类型属性?
提前感谢,Dan

   [XmlAttribute(AttributeName = "Testing")]
   public string Testing
   {
      get { return _testing; }
      set { _testing= value; }
   }
}


Can somebody please tell me if there is a better way of doing this, and how to serialize the Team class which has different containing tags depending on the type attribute?
Thanks in advance, Dan

推荐答案

您不应尝试将XML序列化与此类XML一起使用。它不起作用。

这也是一个糟糕的XML设计。不要使用实际上是有区别的联合的东西,用type属性描述Team元素内容的结构。相反,创建"子类型"。团队,每个团队都有固定的结构。

You should not attempt to use XML Serialization with this sort of XML. It will not work.

This is also a bad design of XML.  Don't use what is, in effect, a discriminated union, describing the structure of the content of the Team element by the type attribute. Instead, create "subtypes" of Team, each of which has a fixed structure.


这篇关于序列化XML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆