Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# XML serialization

I'd like to serialize something like this, where there is a header and a body.

The first part "galleryData" is the header The 2nd part is "imageData" - repeated for each image in gallery

<galleryData>
    <title>some title</title>
    <uuid>32432322</uuid>
    <imagepath>some path</imagepath>
</galleryData>

<imageData>
    <title>title one</title>
    <category>nature</category>
    <description>blah blah</description>
</imageData>

<imageData>
     <title>title two</title>
     <category>nature</category>
     <description>blah blah</description> 
</imageData>

<imageData>
    <title>title three</title>
    <category>nature</category>
    <description>blah blah</description>
</imageData>

I see how to do it if I didn't need a header area. I'm currently just using xmlwriter to create it, but I'd like to serialize the object out to xml instead.

like image 627
mottai Avatar asked Apr 08 '26 19:04

mottai


2 Answers

You need a root in order to have valid XML. Here's an example of how your model might look 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 simply create an instance of this model and serialize it to 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);
    }
}
like image 166
Darin Dimitrov Avatar answered Apr 10 '26 09:04

Darin Dimitrov


Given the way that XML serialization works, I do not believe the structure you are looking for will be possible from a straight Object -> XML structure as in your example you have more than one root node.

If you had something where there was a container node, then individual ImageData elements within them, or a single over arching element to bundle them together you might be able to get by with it.

like image 33
Mitchel Sellers Avatar answered Apr 10 '26 10:04

Mitchel Sellers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!