[XML Records] [Xml Records] how to skip empty substructure with RecordToXml?
Service icon
Forge component by Afonso Carvalho
Application Type

We have some xsd's with both required and optional sections and would like to generate xmls conforming to the xsd's with the optional sections removed when empty by using RecordToXml.

We try to do this by setting ExcludeIfNull to False in the DefaultXmlConfig and adding attributesconfigs for the optional sections with ExcludeIfNull set to True.
This works fine for properties of the Text type, but we also have properties of other types (decimal, date, even entire structures).

We attempted to set the DefaultValue to the innerxml of the serialisation of the inner structure (by hand for now), but that didn't work.

We currently have structures like this:

  1. Main
    • ReqStruc : SubStruc
    • OptStruc  : SubStruc
  2. SubStruc
    • ReqField : Integer
    • OptField : Integer

An example xml, fully filled:

<Main xmlns="uri:something"><ReqStruc><ReqField>1</ReqField><OptField>2</OptField></Req><OptStruc><ReqField>3</ReqField><OptField>4</OptField></OptStruc></Main>

An example xml, everything with default value:

<Main xmlns="uri:something"><ReqStruc><ReqField>0</ReqField></Req></Main>

So OptField should drop out of the ReqStruc and the entire OptStruc should be skipped when empty.

We have custom code to remove the empty sections with XmlElement_Remove, but that is getting rather unwieldy and we would like to get rid of that code in favor of the XmlElement configuration.

Is it possible to configure Xml Records to get those results?

Hello Marijn,

I’ve never encountered this requirement when working with XML Records so I’m not completely certain, but I don’t think this is possible. The ExcludeIfNull property is only fillable for fields and not entire structures. I don’t think there’s any code to check if a structure is completely empty for removal inside the extension either.

I’m going to run some tests and worst case scenario, if this does turn out impossible to implement currently, I’ll look into the extension code to see if we can add that feature.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.