我的解决方案中有多个项目,并且我对每个客户请求的自定义项使用了替代项,以使代码尽可能地井井有条。在这个特定的替代中,我试图从基类继承以进行序列化,但是在尝试进行序列化时遇到错误。
//In the Base project serialization works 100%
namespace Template
{
//[XmlInclude(typeof(StaticText))]
//[XmlInclude(typeof(BoundText))]
//[XmlInclude(typeof(StaticImage))]
//[XmlInclude(typeof(BoundImage))]
//[XmlInclude(typeof(Font))]
public class objType
{
public float X { get; set; }
public float Y { get; set; }
public float Height { get; set; }
public float Width { get; set; }
}
public class ObjMain()
{
public string ObjName { get; set; }
public List<objType> ObjTypeItems { get; set; }
}
public class DoWork() {
using (System.IO.TextReader tr = new System.IO.StringReader(templateXML))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ObjMain));
bg = (ObjMain)serializer.Deserialize(tr);
}
}
}
/*
<ObjMain>
<ObjName>Section 1</ObjName>
<ObjTypeItems>
<objType xsi:type="StaticText">
<X>32.5</X>
<Y>4.35</Y>
<Height>10</Height>
<Width>20</Width>
<TextFieldName>M</TextFieldName>
</objType>
<objType xsi:type="BountText">
<X>32.5</X>
<Y>4.35</Y>
<Height>10</Height>
<Width>20</Width>
<TextFieldName>Mat</TextFieldName>
</objType>
</ObjTypeItems>
</ObjMain>
*/
namespace CustomTemplate
{
[XmlInclude(typeof(StaticText))]
[XmlInclude(typeof(BoundText))]
[XmlInclude(typeof(StaticImage))]
[XmlInclude(typeof(BoundImage))]
[XmlInclude(typeof(Font))]
public class CustomObjType : objType
{
public string GroupName { get; set; }
}
public class CustomObjMain()
{
public string CObjName { get; set; }
public List<CustomObjType> CObjTypeItems { get; set; }
}
public class DoWork() {
using (System.IO.TextReader tr = new System.IO.StringReader(templateXML))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(CustomObjMain));//Error
bg = (CustomObjMain)serializer.Deserialize(tr);
}
}
}
/*
<CustomObjMain>
<CObjName>Section 1</CObjName>
<ObjTypeItems>
<CustomObjType xsi:type="StaticText"> <!--Error when with Serialization -->
<X>32.5</X>
<Y>4.35</Y>
<Height>10</Height>
<Width>20</Width>
<TextFieldName>M</TextFieldName>
<GroupName>MainGroup</GroupName>
</CustomObjType>
<CustomObjType xsi:type="BountText">
<X>32.5</X>
<Y>4.35</Y>
<Height>10</Height>
<Width>20</Width>
<TextFieldName>Bar</TextFieldName>
<GroupName>MainGroup</GroupName>
</CustomObjType>
<CustomObjType xsi:type="BountText">
<X>32.5</X>
<Y>4.35</Y>
<Height>10</Height>
<Width>20</Width>
<TextFieldName>Foo</TextFieldName>
<GroupName>SubGroupFoo</GroupName>
</CustomObjType>
</ObjTypeItems>
</CustomObjMain>
*/
我的感觉是类属性未随基类继承。但是我也尝试将属性添加到从基类继承的类中,但是很幸运,该错误继续在第一行的xml中显示,并在其中添加了“ xsi:type =”。
答案 0 :(得分:0)
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication5
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
ObjMain objects = new ObjMain()
{
ObjName = "Main",
objects = new List<ObjMain>() {
new objType1 { ObjName = "type 1" , X = 1.0F, Y = 2.0F, Height = 5F, Width = 10F},
new objType2 { ObjName = "type 2" , X = 1.5F, Y = 2.5F, Height = 5.5F, Width = 10.5F}
}
};
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(ObjMain));
serializer.Serialize(writer, objects);
}
}
public class objType1 : ObjMain
{
public float X { get; set; }
public float Y { get; set; }
public float Height { get; set; }
public float Width { get; set; }
}
public class objType2 : ObjMain
{
public float X { get; set; }
public float Y { get; set; }
public float Height { get; set; }
public float Width { get; set; }
}
[XmlInclude(typeof(objType1))]
[XmlInclude(typeof(objType2))]
public class ObjMain
{
public string ObjName { get; set; }
[XmlElement()]
public List<ObjMain> objects { get; set; }
}
}