none
XML unknown element handling in WCF RRS feed

  • Question

  • Is there any possible way to handle unknown elements while deserializing SOAP message in WCF service? I am using XmlSerializerFormat in service so XmlSerializer is used do serialize/deserialize messages. Can I use UnknoweNode event?

    Node ordering is important. Now, after deserializing a message with "extra" node, all nodes placed after this node have null value.

    I have a sample class:

    [XmlType(Namespace = "http://mynamespace.com")]
    public class MyClass
    {
        [XmlElement(Form = XmlSchemaForm.Unqualified, Order = 0)]
        public string prop1 { get; set; }
    
        [XmlElement(Form = XmlSchemaForm.Unqualified, Order = 1)]
        public string prop2 { get; set; }
    
        [XmlElement(Form = XmlSchemaForm.Unqualified, Order = 2)]
        public string prop3 { get; set; }
    }

    Following xml is deserialized correctly:

    <MyClass>
        <prop1>valueA</prop1>
        <prop2>valueB</prop2>
        <prop3>valueC</prop3>
    </MyClass>

    But when someone puts an "extra" element, then prop2 and prop3 have null value:

    <MyClass>
        <prop1>valueA</prop1>
        <prop4>valueD</prop4>
        <prop2>valueB</prop2>
        <prop3>valueC</prop3>
    </MyClass>
    How can I skip unnecessary elements during WCF deserialization (using XmlSerializer).
    Sunday, October 11, 2015 10:07 PM

Answers

  • Hi crocodayl,

    According to your description, as far as I know , I am not sure has any way

    to handle  unknown elements while deserializing SOAP message in WCF service.

    Because, if we use the unknown elements the client can not known that is what.

    So, that means the contract is not  correctly.

    About skip unnecessary elements during WCF deserialization, in my opinion is that

    we can modify the data before the deserialization. Because the deserialization is the

    only conversion of the  data to property.

    For more information, please refer to the following articles:

    1.Serialization and Deserialization

    2.WCF xmlSerializer and Data Contract Attributes

    Best Regards,

    Grady

    Wednesday, October 14, 2015 7:31 AM
    Moderator