1
FromXml Bug - IgnoreUnknownElements also ignores known elements
Problem reported by Irwin Plitt - 11/8/2019 at 9:09 PM
Resolved
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:schema>
  <xs:element name="Car">
    <xs:complexType>
      <xs:sequence>
        <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" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This is the xml sent to the FromXml method:

 <Car><Make>VW</Make><abc>def</abc><Color>White</Color><Owner>Joe</Owner></Car>

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

<Car>
    <Make>VW</Make>
    <Color>White</Color>
    <Owner>Joe</Owner>
</Car>

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

<Car>
    <Make>VW</Make>
    <Owner>Joe</Owner>
</Car>


3 Replies

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

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:

<Car><Make>VW</Make><Color>White</Color><abc>def</abc><Owner>Joe</Owner></Car>

It works as expected.

0
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