序列化imageList属性

时间:2014-06-11 23:50:50

标签: c# xmlserializer imagelist

我有一个Album类,其中包含封面图像索引的标题,描述和整数值。它还包含一个imageList。

我需要序列化这个obj,而imageList不是可序列化的,但是imageListStream是......

我是一个完整的c#noob,所以会很感激可用的指导。

期望的结果可能是:

<Album>
    <Title>Album Title</Title>
    <Description>Some explanation.</Description>
    <CoverImgIndx>2</CoverImgIdx>
    <Images>
        <Image>
            <indx>0</indx>
            <filepath>"C:\Images\file1.jpg"</filepath>
        </Image>
        <Image>
            <indx>1</indx>
            <filepath>"C:\Images\file2.png"</filepath>
        </Image>
        <Image>
            <indx>2</indx>
            <filepath>"C:\Images\file3.jpg"</filepath>
        </Image>
        <Image>
            <indx>3</indx>
            <filepath>"C:\Images\file4.bmp"</filepath>
        </Image>
    </Images>
</Album>

显然我需要在反序列化时重新构建imageList ...

2 个答案:

答案 0 :(得分:0)

听起来您只需要使用XmlSerializer类将Album类序列化为XML。

这样的事情应该有效:https://dotnetfiddle.net/yE8RAl

答案 1 :(得分:0)

一般来说,您可以像这样定义您的相册/图片:

[XmlType(TypeName = "Image")]
public class ImageSerializationContainer
{
    [XmlElement(ElementName = "indx")]
    public int Index { get; set; }
    [XmlElement(ElementName = "filepath")]
    public string FilePath { get; set; }
}
[XmlType(TypeName = "Album")]
public class AlbumSerializationContainer
{
    public string Title { get; set; }
    public string Description { get; set; }
    public int CoverImgIndx { get; set; }
    public List<ImageSerializationContainer> Images { get; set; }
}

然后像这样使用XmlSerializer

XmlSerializer ser = new XmlSerializer(typeof(AlbumSerializationContainer));
StringWriter sw = new StringWriter();
ser.Serialize(sw, yourObjectToSerialize);
return sw.ToString();

但是我猜你真正想要的是以某种形式转换你可以序列化的形式的现有类。

不幸的是,你不会在你的问题中付出太多代价,所以我必须做出一些假设。现在,这些假设中的一些肯定是错误的,但这与您提供的信息水平有关。

我将假设您正在使用ImageList类,并且您示例中的文件名存储在附加到ImageList Tag property的字符串数组中。随意修改代码以从其他地方获取此值。您没有提供有关如何构建索引值的信息。我假设indexes中的ImageList.Images collectionAutoMapper

有几种方法可以解决您的问题。我将向您展示如何使用XmlSerializer.Deserialize将您的类转换为可以序列化然后序列化的类。

下面你可以找到一个完整的例子。请注意,如果您的课程与下面的课程不同,您可能需要更新AutoMapper配置。

我演示了两者:将类序列化为XML(示例1)并将另一个类转换为可序列化的类(示例2)。

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Collections.Generic;
using AutoMapper;

namespace SO24174411
{
    class Program
    {
        static void Main()
        {
            AlbumSerializationContainer example1 = new AlbumSerializationContainer
            {
                CoverImgIndx = 2,
                Description = "Some explanation.",
                Images = new List<ImageSerializationContainer>
                {
                    new ImageSerializationContainer {FilePath = @"C:\Images\file1.jpg", Index = 0},
                    new ImageSerializationContainer {FilePath = @"C:\Images\file2.png", Index = 1},
                    new ImageSerializationContainer {FilePath = @"C:\Images\file3.jpg", Index = 2},
                    new ImageSerializationContainer {FilePath = @"C:\Images\file4.bmp", Index = 3}
                },
                Title = "Album Title"
            };
            Console.WriteLine("Example 1");
            Console.WriteLine(Serialize(example1));

            Album album = new Album
            {
                CoverImgIndx = 2,
                Description = "Some explanation.",
                Images = new ImageList(),
                Title = "Album Title"
            };
            SetImages(album.Images, new[]
            {
                @"C:\Images\file1.jpg", 
                @"C:\Images\file1.jpg", 
                @"C:\Images\file2.png", 
                @"C:\Images\file4.bmp"
            });

            var example2 = PerformMapping(album);
            Console.WriteLine("Example 2");
            Console.WriteLine(Serialize(example2));

        }

        private static AlbumSerializationContainer PerformMapping(Album album)
        {
            Mapper.CreateMap<Album, AlbumSerializationContainer>();
            Mapper.CreateMap<ImageList, List<ImageSerializationContainer>>().ConvertUsing<ImageListconverter>();
            AlbumSerializationContainer example2 = Mapper.Map<AlbumSerializationContainer>(album);
            return example2;
        }

        public class ImageListconverter : TypeConverter<ImageList, List<ImageSerializationContainer>>
        {
            protected override List<ImageSerializationContainer> ConvertCore(ImageList source)
            {
                if (source == null)
                {
                    return null;
                }
                List<ImageSerializationContainer> result = new List<ImageSerializationContainer>();
                for (int i = 0; i < source.Images.Count; i++)
                {
                    result.Add(new ImageSerializationContainer { FilePath = ((string[])source.Tag)[i], Index = i });
                }
                return result;
            }
        }

        public static string Serialize(AlbumSerializationContainer a)
        {
            XmlSerializer ser = new XmlSerializer(typeof(AlbumSerializationContainer));
            StringWriter sw = new StringWriter();
            ser.Serialize(sw, a);
            return sw.ToString();
        }

        public static void SetImages(ImageList l, string[] names)
        {
            l.Tag = names;
            for(int i=0;i<names.Length;i++)
            {
                // Aparently you can read names[i] file here if you want
                l.Images.Add(new Bitmap(1, 1));
            }
        }
    }


    public class Album
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public int CoverImgIndx { get; set; }
        public ImageList Images { get; set; }

    }

    [XmlType(TypeName = "Image")]
    public class ImageSerializationContainer
    {
        [XmlElement(ElementName = "indx")]
        public int Index { get; set; }
        [XmlElement(ElementName = "filepath")]
        public string FilePath { get; set; }
    }
    [XmlType(TypeName = "Album")]
    public class AlbumSerializationContainer
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public int CoverImgIndx { get; set; }
        public List<ImageSerializationContainer> Images { get; set; }
    }
}

您将能够使用{{3}}轻松地将xml反序列化回序列化容器类。还需要做一些工作才能将这些工作映射回您所需的课程。由于这部分比你没有提供的任何其他信息更多,所以我把它留作读者的练习。