下面的程序是我在C#中反序列化XML时遇到的一个问题的例子。我有两个单独的程序集,声明一个具有相同名称的类型,'国家/地区'在下面的例子中。这些类型由XML命名空间区分。当我反序列化包含单个' Country'的配置文件时元素那么正确的国家'类型已解决。但是,如果我反序列化列表' '国家'那些元素然后是错误的国家'类型被反序列化。
class Program
{
static void Main(string[] args)
{
XDocument gbConfig = XDocument.Parse(@"<TradingBlocConfiguration>
<GreatBritain>
<Country/>
<Countries>
<Country/>
<Country/>
</Countries>
</GreatBritain> </TradingBlocConfiguration>");
XDocument euConfig = XDocument.Parse(@"<TradingBlocConfiguration>
<EuropeanUnion>
<Country/>
<Countries>
<Country/>
<Country/>
</Countries>
</EuropeanUnion> </TradingBlocConfiguration>");
var greatBritainConfiguration = BuildConfig<TradingBlocConfiguration>(gbConfig);
// A single 'Country' is always deserialized correctly..
Console.WriteLine("Great Britain Country Type " + greatBritainConfiguration.TradingBlocConfig.MemberCountry.GetType());
// A List of 'Country' is deserialized to the wrong type, depending on what '[XmlElement]' tag is listed first.
Console.WriteLine("Great Britain Countries Type " + greatBritainConfiguration.TradingBlocConfig.MemberCountries[0].GetType());
var euConfiguration = BuildConfig<TradingBlocConfiguration>(euConfig);
Console.WriteLine("EU Country Type " + euConfiguration.TradingBlocConfig.MemberCountry.GetType());
Console.WriteLine("EU Countries Type " + euConfiguration.TradingBlocConfig.MemberCountries[0].GetType());
Console.ReadLine();
}
private static T BuildConfig<T>(XDocument doc) where T : class
{
var stream = new MemoryStream();
doc.Save(stream);
T result;
using (var reader = new StreamReader(stream))
{
stream.Position = 0;
var xs = new XmlSerializer(typeof(T));
result = (T)xs.Deserialize(reader);
}
return result;
}
}
[XmlRoot("TradingBlocConfiguration")]
public sealed class TradingBlocConfiguration
{
[XmlElement("GreatBritain", typeof(GB.GreatBritain))]
[XmlElement("EuropeanUnion", typeof(EU.EuropeanUnion))]
public TradingBloc TradingBlocConfig { get; set; }
}
[XmlRoot]
[XmlInclude(typeof(GB.GreatBritain))]
[XmlInclude(typeof(EU.EuropeanUnion))]
public class BaseCountry { }
public abstract class TradingBloc
{
[XmlIgnore]
public abstract List<BaseCountry> MemberCountries { get; set; }
[XmlIgnore]
public abstract BaseCountry MemberCountry { get; set; }
}
namespace GB
{
[XmlRoot("GreatBritain")]
public class GreatBritain : TradingBloc
{
[XmlElement("Country", typeof(Country))]
public override BaseCountry MemberCountry { get; set; }
[XmlArray("Countries")]
[XmlArrayItem("Country", typeof(Country))]
public override List<BaseCountry> MemberCountries { get; set; }
[XmlRoot(Namespace = "GB")]
public class Country : BaseCountry { }
}
}
namespace EU
{
[XmlRoot("EuropeanUnion")]
public class EuropeanUnion : TradingBloc
{
[XmlElement("Country", typeof(Country))]
public override BaseCountry MemberCountry { get; set; }
[XmlArray("Countries")]
[XmlArrayItem("Country", typeof(Country))]
public override List<BaseCountry> MemberCountries { get; set; }
[XmlRoot(Namespace = "EU")]
public class Country : BaseCountry { }
}
}
如果您运行上面的示例,则输出为:
Great Britain Country Type XmlSerializationTests.GB.GreatBritain+Country
Great Britain Countries Type XmlSerializationTests.EU.EuropeanUnion+Country
EU Country Type XmlSerializationTests.EU.EuropeanUnion+Country
EU Countries Type XmlSerializationTests.EU.EuropeanUnion+Country
&#39;英国国家类型&#39;是不正确的。如果您更改TradingBlocConfiguration类中的[XmlElement]属性的顺序,如:
[XmlRoot("TradingBlocConfiguration")]
public sealed class TradingBlocConfiguration
{
[XmlElement("EuropeanUnion", typeof(EU.EuropeanUnion))]
[XmlElement("GreatBritain", typeof(GB.GreatBritain))]
public TradingBloc TradingBlocConfig { get; set; }
}
然后结果变为:
Great Britain Country Type XmlSerializationTests.GB.GreatBritain+Country
Great Britain Countries Type XmlSerializationTests.GB.GreatBritain+Country
EU Country Type XmlSerializationTests.EU.EuropeanUnion+Country
EU Countries Type XmlSerializationTests.GB.GreatBritain+Country
在这种情况下,英国看起来不错,但欧盟错了:)。任何人都可以解释为什么List被反序列化为错误的类型?
答案 0 :(得分:0)
解决方案是添加xmlns标记。下面更新的代码可以正常工作:
data.table