Correct XML needs a root. Here is an example of what your model looks like:
public class ImageData
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("category")]
public string Category { get; set; }
[XmlElement("description")]
public string Description { get; set; }
}
public class GalleryData
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("uuid")]
public string UUID { get; set; }
[XmlElement("imagepath")]
public string ImagePath { get; set; }
}
public class MyData
{
[XmlElement("galleryData")]
public GalleryData GalleryData { get; set; }
[XmlElement("imageData")]
public ImageData[] ImageDatas { get; set; }
}
and then just instantiate this model and serialize it into a stream:
class Program
{
static void Main()
{
var myData = new MyData
{
GalleryData = new GalleryData
{
Title = "some title",
UUID = "32432322",
ImagePath = "some path"
},
ImageDatas = new[]
{
new ImageData
{
Title = "title one",
Category = "nature",
Description = "blah blah"
},
new ImageData
{
Title = "title two",
Category = "nature",
Description = "blah blah"
},
}
};
var serializer = new XmlSerializer(myData.GetType());
serializer.Serialize(Console.Out, myData);
}
}
source
share