[英]C# Adding element to collection
我正在尝试将XmlElement添加到XmlElement [],但这似乎是不可能的。
错误消息:
无法将类型'System.Xml.XmlElement'隐式转换为'System.Xml.XmlElement []'
XmlElement []对象没有添加或插入功能
那我该怎么办呢?
用代码更新:
这部分是从XSD私有System.Xml.XmlElement [] anyField创建的;
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Any
{
get
{
return this.anyField;
}
set
{
this.anyField = value;
}
}
在这里,我试图创建对象并将UniversalShipment添加到Any集合中。
UniversalInterchangeBody body = new UniversalInterchangeBody();
UniversalShipmentData shipmentData = new UniversalShipmentData();
XmlElement universalShipmentXML = SerializeToXmlElement(shipmentData);
body.Any = universalShipmentXML;
public static XmlElement SerializeToXmlElement(object o)
{
XmlDocument doc = new XmlDocument();
using (XmlWriter writer = doc.CreateNavigator().AppendChild())
{
new XmlSerializer(o.GetType()).Serialize(writer, o);
}
return doc.DocumentElement;
}
我会使用一个列表。
List<XmlElement> elements = new List<XmlElement>();
elements.Add(xamlElement);
预先调整数组的大小,并在数组的所有条目中使用默认元素。 您不能通过插入/添加来调整数组的大小(如果确实要这样做,请使用List<T>
代替)。 否则,只需在特定索引处设置条目的值即可:
array[index] = value
是的,的确是将XmlElement添加到XmlElement []的副本,对此深感抱歉!
至于在这种情况下我的代码的解决方案:
var uShipmentCollection = new UniversalShipmentData[]
{
shipmentData
};
感谢您提供重复的Gusdor参考!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.