问题描述
在便携式类库,我有一个包含有一个 XmlAnyElement将
属性的成员的类。
In a Portable Class Library, I have a class that contains a member with an XmlAnyElement
attribute.
public partial class VariableWebServiceResponse {
private List<System.Xml.XmlElement> anyField;
public VariableWebServiceResponse () {
this.anyField = new List<System.Xml.XmlElement>();
}
[System.Xml.Serialization.XmlAnyElementAttribute(Order=0)]
public List<System.Xml.XmlElement> Any {
get {
return this.anyField;
}
set {
this.anyField = value;
}
}
}
这类型的类作品完美在.NET 4.0中,所以我有这样的代码:
This type of class works perfectly in .NET 4.0 so I have code like:
private T Deserialize<T>(VariableWebServiceResponse response)
{
var name = typeof(T).Name;
var element = response.Any.SingleOrDefault(x => x.Name == name);
return Deserialize<T>(element.OuterXml);
}
private static T Deserialize<T>(string xml)
{
return (T)new XmlSerializer(typeof(T)).Deserialize(new StringReader(xml));
}
现在的问题似乎是,的XmlElement
不是在PCL支持。所以,
怎么能实现在PCL同样的结果?
The problem now seems to be that XmlElement
is not supported in a PCL.So how can one achieve the same results in a PCL?
推荐答案
很抱歉这么晚才回复。正如你已经注意到你不能在便携使用的XmlElement,这是由于它仅是.NET Framework中可用。 Silverlight的,手机和Windows Store应用程序不公开此类型。
Sorry for the late reply. As you've noticed you can't use XmlElement in portable, this is due to it only being available in .NET Framework. Silverlight, Phone and Windows Store apps do not expose this type.
不过,我们确实有替代品,便携式定位时.NET 4.0.3及以上(这是需要得到XLINQ支持),您可以使用的XElement *与XmlAnyElementAttribute的替代品。
However, we do have a replacement, when targeting .NET 4.0.3 and above in portable (which is required to get XLINQ support), you can use XElement* as a replacement with XmlAnyElementAttribute.
*我刚刚提交了错误的文档,以更清楚这一点了。
* I just filed a bug to make this a little clearer in the docs.
这篇关于便携式类库,XmlAnyElementAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!