سؤال

[XmlRoot("company"), DataContract(Name = "company")]
public class Company : IProvideSerialization
{
    /// <summary>
    /// Stock exchange the company is in.
    /// </summary>
    /// <see cref="https://developer.linkedin.com/documents/company-lookup-api-and-fields"/>
    /// <remarks>Available only for public companies.</remarks>
    [XmlElement("stock-exchange"), DataMember(Name = "stock-exchange", EmitDefaultValue = false, IsRequired = false), EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
    protected SerializableEnum<StockExchange> StockExchangeForXML;
    public static Company FromXml(String XML)
    {
        XmlSerializer x = new XmlSerializer(typeof(Company));
        return (Company)x.Deserialize(new StringReader(XML));
    }
}

The SerializableEnum implements IXmlSerializable.

SerializableEnum also has a FromXml that works; it looks as followed:

    public static SerializableEnum<T> FromXml(string XML)
    {
        XmlRootAttribute XR = (XmlRootAttribute)System.Attribute.GetCustomAttribute(typeof(T), typeof(XmlRootAttribute));
        XmlSerializer x = new XmlSerializer(typeof(SerializableEnum<T>), new XmlRootAttribute() { ElementName = XR.ElementName, IsNullable = true });
        return (SerializableEnum<T>)x.Deserialize(new StringReader(XML));
    }

When I do:

        String StockXML = "<stock-exchange><code>NMS</code><name>NASDAQ</name></stock-exchange>";
        String CompanyXML = "<company><stock-exchange><code>NMS</code><name>NASDAQ</name></stock-exchange></company>";

        SerializableEnum<StockExchange> Stock = SerializableEnum<StockExchange>.FromXml(StockXML);
        Company Cmp = Company.FromXml(CompanyXML);

Stock will be populated with the data, but Cmp will not have the Stock data populated (looks like ReadXml never gets called)...

I've tried adding additional types to the XmlSerializer (like , new Type[] {typeof(SerializableEnum)}), but that doesn't help.

What am I missing here? Thanks.

I think it might be something like that RootElementAttribute that I had to add in the FromXml in the SerializableEnum class. The IXmlSerializable ignored the XmlRoot attribute that the enum had, so I added that code to add it when deserializing. Is there a different way to make this all work togeather?

هل كانت مفيدة؟

المحلول

I presume that Company class does NOT implement IXmlSerializable.

Default Xml serialization ignores all non-public and readonly members, so it ignores protected StockExchangeForXML.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top