在尝试让我的编程反序列化多个XmlArrayItem时,我遇到了相当愚蠢的问题。
XML基本上是这样的:
<Root2>
<Data2>
<HOLD>
...
</HOLD>
<CUST_HOLD>
...
</CUST_HOLD>
</DATA2>
</ROOT2>
我的可序列化代码如下:
[Serializable()]
[System.Xml.Serialization.XmlRoot("Root2")]
public class Root2
{
[System.Xml.Serialization.XmlArray("Data2")]
[System.Xml.Serialization.XmlArrayItem("CUST_HOLD", typeof(CUST_HOLD))]
public CUST_HOLD[] CUST_HOLD { get; set; }
[System.Xml.Serialization.XmlArrayItem("HOLD", typeof(HOLD))]
public HOLD[] HOLD { get; set; }
}
我尝试了不同的配置,但这是唯一一个不会导致错误的配置。 但问题是,只有第一个XmlArrayItem被处理(在本例中为CUST_HOLD)。 另一个保持为null,而相应数组中至少应有一个项目。
答案 0 :(得分:0)
如果您无法为其编写xsd,则无法将其序列化/反序列化为xml。
这将是xsd中的任何序列或极其糟糕的集合。
答案 1 :(得分:0)
试试,
<强> CODE 强>
课程声明
[Serializable()]
[System.Xml.Serialization.XmlInclude(typeof(CUST_HOLD))]
[System.Xml.Serialization.XmlInclude(typeof(HOLD))]
[System.Xml.Serialization.XmlType(TypeName = "Data2")]
public class Root2
{
[System.Xml.Serialization.XmlArrayItem("CUST_HOLD")]
public CUST_HOLD[] CUST_HOLD;
[System.Xml.Serialization.XmlArrayItem("HOLD")]
public HOLD[] HOLD;
}
[Serializable()]
[System.Xml.Serialization.XmlType("CUST_HOLD")]
public class CUST_HOLD
{
public int i;
}
[Serializable()]
[System.Xml.Serialization.XmlType("HOLD")]
public class HOLD
{
public int i;
}
<强>序列化强>
List<Root2> list = new List<Root2>();
Root2 obj = new Root2();
obj.CUST_HOLD = new CUST_HOLD[] { new CUST_HOLD() { i = 1 }, new CUST_HOLD() { i = 1 }, new CUST_HOLD() { i = 1 } };
obj.HOLD = new HOLD[] { new HOLD() { i = 1 }, new HOLD() { i = 1 }, new HOLD() { i = 1 } };
list.Add(obj);
//Serialize List<Root2>
System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<Root2>),new System.Xml.Serialization.XmlRootAttribute("Root2"));
System.IO.MemoryStream mo = new System.IO.MemoryStream();
Serializer.Serialize(mo, list);
string str = UnicodeEncoding.UTF8.GetString(mo.ToArray());
<强>输出:强>
<?xml version="1.0"?>
<Root2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Data2>
<CUST_HOLD>
<CUST_HOLD>
<i>1</i>
</CUST_HOLD>
<CUST_HOLD>
<i>1</i>
</CUST_HOLD>
<CUST_HOLD>
<i>1</i>
</CUST_HOLD>
</CUST_HOLD>
<HOLD>
<HOLD>
<i>1</i>
</HOLD>
<HOLD>
<i>1</i>
</HOLD>
<HOLD>
<i>1</i>
</HOLD>
</HOLD>
</Data2>
</Root2>
<强>反序列化强>
string str = @"<?xml version='1.0'?>
<Root2 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<Data2>
<CUST_HOLD>
<CUST_HOLD>
<i>1</i>
</CUST_HOLD>
<CUST_HOLD>
<i>1</i>
</CUST_HOLD>
<CUST_HOLD>
<i>1</i>
</CUST_HOLD>
</CUST_HOLD>
<HOLD>
<HOLD>
<i>1</i>
</HOLD>
<HOLD>
<i>1</i>
</HOLD>
<HOLD>
<i>1</i>
</HOLD>
</HOLD>
</Data2>
</Root2>";
System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<Root2>), new System.Xml.Serialization.XmlRootAttribute("Root2"));
System.IO.MemoryStream mo = new System.IO.MemoryStream(UnicodeEncoding.UTF8.GetBytes(str));
List<Root2> list = (List<Root2>)Serializer .Deserialize(mo);
注意 <Data2>
标记仅在单个List<Root2>
实例Root2
标记的序列化类型为<Data2>
时才会显示在xml字符串中并且不会反序列化做不到。反序列化将返回CUST_HOLD
&amp;的空值。 HOLD
。返回类型应为List<Root2>
。