[英]How do I include “XmlInclude” on a Class that inherits from a base class with working “XmlInclude” attributes?
我在我的解決方案中有多個項目,我正在使用覆蓋任何自定義單個客戶端請求以保持代碼盡可能有條理。 在這個特定的覆蓋中,我試圖從序列化的基類繼承我將要做但我在嘗試序列化時遇到錯誤。
//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 =”。
試試以下:
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; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.