Portable Class Library, XmlAnyElementAttribute

放肆的年华 提交于 2020-01-02 16:22:07

问题


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;
        }
    }
}

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));
    }

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/12301157/portable-class-library-xmlanyelementattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!