C # XML serialization

I would like to serialize something like this where there is a header and a body.

The first part of "galleryData" is the title. The second part - "imageData" - is repeated for each image in the 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 this if I do not need the header area. I am currently just using xmlwriter to create it, but I would like to serialize the object instead of xml.

+3
source share
6 answers

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);
    }
}
+6
source

, XML, , , , Object → XML, node.

-, node, ImageData , , .

+1

XmlSerialization, - , .

Data imageData , .

0

, XmlSerializabe List, xml

0

.NET XML- :

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

For your XML document, you need the root element, and if your current model does not match the required serialized form, create and fill in the appropriate data transfer object to complete the serialization job.

0
source

The structure you are showing is invalid XML because it contains more than one root root, so you can forget about the XmlSerializer. If you want to easily handle similar xml structures, I suggest the Html Agility Pack

0
source

All Articles