FromXml Bug - IgnoreUnknownElements also ignores known elements
Problem reported by Irwin Plitt - 11/8/2019 at 9:09 PM
FromXml does not work properly when the SerializationContext.IgnoreUnknownElements option is used.

Specifically, when an unknown element is present and it precedes two or more optional elements, the first optional element is also ignored.

The car.xsd is defined as:
  <xs:element name="Car">
        <xs:element name="Make" type="xs:string" />
        <xs:element name="Color" type="xs:string" minOccurs="0" />
        <xs:element name="Owner" type="xs:string" minOccurs="0" />

This is the xml sent to the FromXml method:


After using the FromXml method and specifying IgnoreUnknownElements=true, and then using the ToXml method, the expected result is:


but the actual xml is missing the Color element, the first optional element following the unknown element:


3 Replies

Reply to Thread
Irwin Plitt Replied
I have a demonstration project that illustrates this issue at github.com/iplitt/liquid-technologies-2019-bug
Liquid Support Replied
Employee Post

Sorry for the delayed response.

I am able to replicate this issue and have raised this with the dev team.

Interestingly if you move the unknown item as follows:


It works as expected.

Liquid Support Replied
Employee Post Marked As Resolution
Hello again,

The dev team responded to this issue saying this is a limitation of using Optional elements.

I.e. the parser looks for <Color> and finds <abc>, as Color is optional it moves to the next element. It then tries to read <Owner>, and treats <abc> and <Color> as unknown elements.

This issue could be resolved by implementing a look ahead function, but this would impact performance and would also not work for repeating groups.

So the bottom line is, IgnoreUnknownElements will only work correctly for Mandatory elements, or where the optional item is the last item in the sequence.

Another option would be to move to the new Liquid XML Objects and use the 'Simplified Model':

This flattens the sequence and should read the unknown elements as described here:

Reply to Thread