c#中的Xml序列化,数组的内容占位符
我试图通过序列化对象来创建xml文档。我遇到了一个问题 目标xml结构类似于HTML页面。它有一个带有一些属性的表单元素 可以有任意数量的控件,如文本字段、按钮等。我为这个结构创建的对象如下所示。为了添加所有这些控件,我使用了一个名为items的数组列表。当对象被序列化时,所有控件都显示在标记内。我希望控件显示为表单元素的直接子级。我该怎么做c#中的Xml序列化,数组的内容占位符,c#,generics,serialization,C#,Generics,Serialization,我试图通过序列化对象来创建xml文档。我遇到了一个问题 目标xml结构类似于HTML页面。它有一个带有一些属性的表单元素 可以有任意数量的控件,如文本字段、按钮等。我为这个结构创建的对象如下所示。为了添加所有这些控件,我使用了一个名为items的数组列表。当对象被序列化时,所有控件都显示在标记内。我希望控件显示为表单元素的直接子级。我该怎么做 [XmlInclude(typeof(Lstatic))] [XmlInclude(typeof(textField))] pu
[XmlInclude(typeof(Lstatic))]
[XmlInclude(typeof(textField))]
public class form
{
[XmlAttribute]
public String action
{
get;
set;
}
[XmlAttribute]
public String method
{
get;
set;
}
[XmlAttribute]
public String name
{
get;
set;
}
[XmlArray]
public ArrayList items
{
get;
set;
}
}
这就是结果XML
<form name="login" method="get" action="/FetchIndex.asmx/findAddresses">
<items>
<anyType value="Please key in your details:" xsi:type="Lstatic"/>
<anyType name="postCode" value="" xsi:type="textField" size="10" label="Postcode:" hint="Enter your postcode"/>
</items>
</form>
相反,我希望得到这样的xml
<form name="login" method="get" action="/FetchIndex.asmx/findAddresses">
<anyType value="Please key in your details:" xsi:type="Lstatic"/>
<anyType name="postCode" value="" xsi:type="textField" size="10" label="Postcode:" hint="Enter your postcode"/>
</form>
我怎样才能在c#中做到这一点?
谢谢此代码未经测试,对不起,这只是我记得的。我相信您需要
xmlement
属性
[XmlElement("AnyType")]
public object[] itemsSerializable
{
get { return items.ToArray(); }
set { items = new ArrayList(value); }
}
[XmlIgnore]
public ArrayList items
{
get;
set;
}
如果它可以工作,那么它也可能在不需要itemsSerializable
属性的情况下工作,您应该检查它