0

I'm trying to add an XmlElement to XmlElement[] but this seems to be impossible.

the errormessage:

Cannot implicitly convert type 'System.Xml.XmlElement' to 'System.Xml.XmlElement[]'

The XmlElement[] object does not have an Add or Insert function

so how can I do this?

Update with code:

This part is created from an XSD private System.Xml.XmlElement[] anyField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyElementAttribute()]
    public System.Xml.XmlElement[] Any
    {
        get
        {
            return this.anyField;
        }
        set
        {
            this.anyField = value;
        }
    }

Here I am trying to create the object and add a UniversalShipment to the Any collection.

    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;
    }
George
  • 76
  • 9

3 Answers3

2

I would use a List.

List<XmlElement> elements = new List<XmlElement>();
elements.Add(xamlElement);
Gordonium
  • 3,389
  • 23
  • 39
1

An array is pre-sized with default elements in all entries of the array. You cannot re-size the array by inserting/adding (if you do want to do this use a List<T> instead). Otherwise simply set the value of an entry at a specific index:

array[index] = value
Slugart
  • 4,535
  • 24
  • 32
0

Yes, it is indeed a copy of Add XmlElement to XmlElement [] dynamically sorry for this!

as for the solution to my code in this case:

var uShipmentCollection = new UniversalShipmentData[]
{
    shipmentData
};

Thanks for the duplicate reference Gusdor!

Community
  • 1
  • 1
George
  • 76
  • 9