Convert an Array or ArrayList to Xml string

I need to convert an array or an arraylist into an xml string
The array is an one dimensional array
LVL 1
countrymeisterAsked:
Who is Participating?
 
crisco96Connect With a Mentor Commented:
As long as your array is serializable (it is made of standard .net types) the below code should work (note that the variable myArray is what contains your array element that you want to convert to xml)
System.IO.MemoryStream stream = new System.IO.MemoryStream();

new System.Xml.Serialization.XmlSerializer(myArray).Serialize(stream, myArray);

stream.Position = 0;

System.IO.StreamReader reader = new System.IO.StreamReader(stream);
string xml = reader.ReadToEnd();

reader.Close();
stream.Close();

Open in new window

0
All Courses

From novice to tech pro — start learning today.