gpt4 book ai didi

c# - 如何序列化 List (Nokia Imaging SDK)?

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:42 25 4
gpt4 key购买 nike

我正在尝试保存一个 IFilter 列表(接口(interface)类型),它使用 XML 序列化应用于图像,以便用户可以从他离开的地方编辑相同的图像。

[XmlRoot]
public class ImageProperties
{
public string ImageName { get; set; }
public string ImageFilePath { get; set; }
public List<IFilter> Filters { get; set; }
}

这可能吗?是否有另一种替代方法可以做到这一点?

最佳答案

您可以使用 IXmlSerializable 来实现这一点。假设您可以更改 ImageProperties 类。

序列化后,您可以通过查看每个过滤器实例并查询它来获取类型。您可以将此类型信息存储在 XML 中,这样当您阅读它时,您就知道它是哪种类型,然后您可以为每个过滤器调用默认的 XML 序列化程序。

这是一个可能的实现。

public class ImageProperties : IXmlSerializable
{
public string ImageName { get; set; }
public string ImageFilePath { get; set; }
public List<IFilter> Filters { get; set; }

public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(System.Xml.XmlReader reader)
{
string startEle = reader.Name;
reader.ReadStartElement();
Filters = new List<IFilter>();

do
{
switch (reader.Name)
{
case "imgName":
ImageName = reader.ReadElementContentAsString();
break;
case "imgFilePath":
ImageFilePath = reader.ReadElementContentAsString();
break;
case "filters":
reader.ReadStartElement("filters");
while (reader.Name.Equals("iFilter"))
{
XmlSerializer filterSerializer = new XmlSerializer(Type.GetType(reader.GetAttribute("type")));
reader.ReadStartElement("iFilter");
Filters.Add((IFilter)filterSerializer.Deserialize(reader));
reader.ReadEndElement();
}
reader.ReadEndElement();
break;

default:
reader.ReadOuterXml();
break;
}

} while (reader.Name != startEle);
}

public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("imgName", ImageName);
writer.WriteElementString("imgFilePath", ImageFilePath);
writer.WriteStartElement("filters");
foreach (IFilter filter in Filters)
{
writer.WriteStartElement("iFilter");
writer.WriteAttributeString("type", filter.GetType().FullName);
XmlSerializer filterSerializer = new XmlSerializer(filter.GetType());
filterSerializer.Serialize(writer, filter);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
}

如果您有不同类型的过滤器,则会调用实际类型的默认序列化程序,因此将记录它们的独特属性。

例如,使用这些过滤器类:

public interface IFilter
{
string SomeCommonProp { get; set;}
}

[XmlRoot("myFilter")]
public class MyFilter : IFilter
{

[XmlElement("somemyFilterProp")]
public string SomeMyFilterProp { get; set; }

[XmlElement("someCommonProp")]
public string SomeCommonProp { get; set;}
}

[XmlRoot("myOtherFilter")]
public class MyOtherFilter : IFilter
{
[XmlElement("someOtherFilterProp")]
public string SomeOtherFilterProp { get; set; }

[XmlElement("someCommonProp")]
public string SomeCommonProp { get; set;}
}

您可以使用如下方法将 IFilter 中的两种不同类型的过滤器序列化和反序列化为 xml。

static void Main(string[] args)
{
ImageProperties props = new ImageProperties();
props.ImageName = "img.png";
props.ImageFilePath = "c:\\temp\\img.png";
props.Filters = new List<IFilter>();
props.Filters.Add(new MyFilter() { SomeMyFilterProp = "x", SomeCommonProp ="p" });
props.Filters.Add(new MyOtherFilter() { SomeOtherFilterProp = "y", SomeCommonProp ="p" });

XmlSerializer s = new XmlSerializer(typeof(ImageProperties));
using (StreamWriter writer = new StreamWriter(@"c:\temp\imgprops.xml"))
s.Serialize(writer, props);

using (StreamReader reader = new StreamReader(@"c:\temp\imgprops.xml"))
{
object andBack = s.Deserialize(reader);
}

Console.ReadKey();
}

这会生成如下所示的 XML。

<?xml version="1.0" encoding="utf-8"?>
<ImageProperties>
<imgName>img.png</imgName>
<imgFilePath>c:\temp\img.png</imgFilePath>
<filters>
<iFilter type="SomeNameSpace.Whatever.MyFilter">
<myFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<somemyFilterProp>x</somemyFilterProp>
<someCommonProp>p</someCommonProp>
</myFilter>
</iFilter>
<iFilter type="SomeNameSpace.Whatever.MyOtherFilter">
<myOtherFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<someOtherFilterProp>y</someOtherFilterProp>
<someCommonProp>p</someCommonProp>
</myOtherFilter>
</iFilter>
</filters>
</ImageProperties>

关于c# - 如何序列化 List<IFilter> (Nokia Imaging SDK)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26982927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com