mottai
mottai

Reputation: 33

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.

Upvotes: 3

Views: 362

Answers (6)

Paweł Smejda
Paweł Smejda

Reputation: 2005

The structure you're showing is not valid XML because is containing more then one root node, so you can forget about XmlSerializer. If you want to handle easily with such a xml similar structures I suggest Html Agility Pack

Upvotes: 0

devdigital
devdigital

Reputation: 34349

You can use something like the following to serialize a .NET object to an XML string:

    protected string ObjectToXml<T>(T obj)
    {
        var sw = new StringWriter();

        try
        {
            var mySerializer = new XmlSerializer(typeof(T));
            mySerializer.Serialize(sw, obj);
        }
        catch (Exception ex)
        {
           // Error logging here
        }

        return sw.ToString();
    }

You will need a root element for your XML document, and if your current model doesn't match the desired serialized form, then create and populate an appropriate data transfer object to do the serialisation job.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

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);
    }
}

Upvotes: 6

fedotoves
fedotoves

Reputation: 767

You can create an object for header with properties an mark the with XmlSerializabe and the add a field of type List so that standart xml serialized will serialize it as child elements of header

Upvotes: 0

dhirschl
dhirschl

Reputation: 2098

If you are using XmlSerialization, you will need a root element - which will be the class that you serialize.

Your galleryData and imageData will be object instance variables within the class used for your root element.

Upvotes: 0

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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.

Upvotes: 1

Related Questions