darthg8r
asked on
Serialize a class
I'm trying to serialize a custom class that needs to use multiple elements of the same name. I've tried using xmlarray, but it wraps them in another element.
I want my xml to look like this.
<xmlroot>
<element>some text</element>
<element>some more text</element>
</xmlroot>
My code:
[Serializable(), XmlRoot("xmlroot")]
public class xmlroot
{
[XmlArray("element")]
public ArrayList MyProp1 = new ArrayList();
public xmlroot()
{
MyProp1.Add("some text");
MyProp1.Add("some more text");
}
}
I've also tried this, but it throws reflection errors when trying to serialize:
[Serializable(), XmlRoot("xmlroot")]
public class xmlroot
{
[XmlElement("element")]
public string MyProp1;
[XmlElement("element")]
public string MyProp2;
public xmlroot()
{
MyProp1 = "some text";
MyProp2 = "some more text";
}
}
After I serialize, it looks like this:
<xmlroot>
<element>
<anyType xsi:type="string">some text</anyType>
<anyType xsi:type="string">some more text</anyType>
</element>
</xmlroot>
I want my xml to look like this.
<xmlroot>
<element>some text</element>
<element>some more text</element>
</xmlroot>
My code:
[Serializable(), XmlRoot("xmlroot")]
public class xmlroot
{
[XmlArray("element")]
public ArrayList MyProp1 = new ArrayList();
public xmlroot()
{
MyProp1.Add("some text");
MyProp1.Add("some more text");
}
}
I've also tried this, but it throws reflection errors when trying to serialize:
[Serializable(), XmlRoot("xmlroot")]
public class xmlroot
{
[XmlElement("element")]
public string MyProp1;
[XmlElement("element")]
public string MyProp2;
public xmlroot()
{
MyProp1 = "some text";
MyProp2 = "some more text";
}
}
After I serialize, it looks like this:
<xmlroot>
<element>
<anyType xsi:type="string">some text</anyType>
<anyType xsi:type="string">some more text</anyType>
</element>
</xmlroot>
You can also feed your XML into the Xsd.exe tool to get the object model (classes) defined for you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What has already been stated is true... As to why you ended up with the xml like you did is that an Array is translated into an XML Sequence and since you did not use an attribute to specify the type of data in the array the translation asumed anyType. Hope this helps...
Try this
[Serializable(), XmlRoot("xmlroot")]
public class XmlRoot: CollectionBase
{
public int Add(Element element)
{
return this.InnerList.Add(element );
}
[XmlArrayItem("Element")]
public Element this[int index]
{
get
{
return this.InnerList[index] as Element ;
}
set
{
this.InnerList[index]=valu e;
}
}
}
[Serializable(), XmlRoot("xmlroot")]
public class XmlRoot: CollectionBase
{
public int Add(Element element)
{
return this.InnerList.Add(element
}
[XmlArrayItem("Element")]
public Element this[int index]
{
get
{
return this.InnerList[index] as Element ;
}
set
{
this.InnerList[index]=valu
}
}
}
An arraylist member is properly serialized as shown below, assuming the objects in the arraylist is of type "Item":
// This attribute enables the ArrayList to be serialized:
[System.Xml.Serialization. XmlArray(" Items")]
// Explicitly tell the serializer to expect the Item class
// so it can be properly written to XML from the collection:
[System.Xml.Serialization. XmlArrayIt em("item", typeof(Ite m))]
public ArrayList myArrayList;
HTH
// This attribute enables the ArrayList to be serialized:
[System.Xml.Serialization.
// Explicitly tell the serializer to expect the Item class
// so it can be properly written to XML from the collection:
[System.Xml.Serialization.
public ArrayList myArrayList;
HTH
[Serializable(), XmlRoot("xmlroot")]
public class xmlroot : CollectionBase
{
[XmlElement("element")]
public string this[int index]
{
get { ... }
set { ... }
}
public int Add( string element ) { ... }
public void Remove( int index ) { ... }
}