1

Context

I have to map data of my own application to a format of a third party to be able to push this data (drop .xml file on FTP server). I have been given XSD files (which have circular references and don't allow me to generate any .cs files) and some XML examples. In these XML examples I see a construction that is clearly meant to reference objects and thus reuse data in that way. See the following 2 lines as a snippet from the full example:

<Keyword Code="Discovery" ID="keyword_136" />

<Keyword Ref="keyword_136" />

Full example: https://filebin.net/3imdjmvxdj37o4ku/Exemple_CatalogueXFT.xml?t=n2qtmi7d

Current situation

My application is written in .NET and thus I have made DTO's to match the XML example / XSD specifications. These DTO's are then serialized using an XmlWriter.

I have create the following example of these "referenced items -> AttributeItem" and then the "list items that reference the AttributeItem --> ListItem". The output comes close to the XML example of the third party, however some XML attributes are there when they shouldn't be.

Construction and serialization

var catalogueTest = new CatalogueRefTest();
XmlWriter.SerializeToXml(catalogueTest, pathTest);

Class and constructor with dummy data

public class RefItemBase
{
    [XmlAttribute("Ref")]
    public string Ref { get; set; }
}

public class AttributeItem : RefItemBase
{
    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlAttribute("ID")]
    public string Id { get; set; }
}

public class ListItem
{
    [XmlArray("Attribute")] //SerializeToRefItem
    public List<RefItemBase> Attributes { get; set; }
}

public class CatalogueRefTest
{
    public CatalogueRefTest()
    {
        this.Attributes = new List<AttributeItem>();
        for (int i = 0; i < 5; i++)
        {
            var uniqueRef = $"attribute_{i}";
            this.Attributes.Add(new AttributeItem
            {
                Id = uniqueRef,
                Ref = uniqueRef,
                Name = $"Attribute with name {i}"
            });
        }

        this.ListItems = new List<ListItem>()
        {
            new ListItem
            {
                Attributes = this.Attributes.GetRange(0,3).Cast<RefItemBase>().ToList()
            },
            new ListItem
            {
                Attributes = this.Attributes.GetRange(1,3).Cast<RefItemBase>().ToList()
            }
        };
    }

    [XmlArray("Attribute")] //SerializeToListDefinition
    public List<AttributeItem> Attributes { get; set; }

    [XmlArray("ListItem")]
    public List<ListItem> ListItems { get; set; }
}

Current output

<?xml version="1.0" encoding="utf-8"?>
<CatalogueRefTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Attribute>
    <AttributeItem Ref="attribute_0" Name="Attribute with name 0" ID="attribute_0" />
    <AttributeItem Ref="attribute_1" Name="Attribute with name 1" ID="attribute_1" />
    <AttributeItem Ref="attribute_2" Name="Attribute with name 2" ID="attribute_2" />
    <AttributeItem Ref="attribute_3" Name="Attribute with name 3" ID="attribute_3" />
    <AttributeItem Ref="attribute_4" Name="Attribute with name 4" ID="attribute_4" />
  </Attribute>
  <ListItem>
    <ListItem>
      <Attribute>
        <RefItemBase xsi:type="AttributeItem" Ref="attribute_0" Name="Attribute with name 0" ID="attribute_0" />
        <RefItemBase xsi:type="AttributeItem" Ref="attribute_1" Name="Attribute with name 1" ID="attribute_1" />
        <RefItemBase xsi:type="AttributeItem" Ref="attribute_2" Name="Attribute with name 2" ID="attribute_2" />
      </Attribute>
    </ListItem>
    <ListItem>
      <Attribute>
        <RefItemBase xsi:type="AttributeItem" Ref="attribute_1" Name="Attribute with name 1" ID="attribute_1" />
        <RefItemBase xsi:type="AttributeItem" Ref="attribute_2" Name="Attribute with name 2" ID="attribute_2" />
        <RefItemBase xsi:type="AttributeItem" Ref="attribute_3" Name="Attribute with name 3" ID="attribute_3" />
      </Attribute>
    </ListItem>
  </ListItem>
</CatalogueRefTest>

Questions

  1. Am I missing something very obvious with this Ref / ID construction? The casing also makes me think this is in some way "custom" and that the third party knows how to read this but it is not any default serialization.

  2. With the above code / examples. How can I omit the "Ref" attribute for the "reference list" and omit the "Code" / "ID" attributes for the "actual list"?

Starceaker
  • 631
  • 2
  • 5
  • 14
  • 1
    You are using only part of the XFT schemas (see : https://repository.data2type.de/XFT/v_1.0/html/index.html). Looks like you need to reference the main schema (xml.xsd) and then include the types you are using. – jdweng Sep 05 '20 at 04:04
  • 1
    Those attributes still need to be configured indeed. What part of my questions will that fix according to you? That's not clear to me at the moment. – Starceaker Sep 05 '20 at 05:26
  • 1
    You need to create the c# classes from the schema using the xsd tool. The schema need additional "include" statements for object defined in other schema. The include is just a reference to another xsd file. It looks like the entire schema is at a French Website that you have to register to get the schema. The link to the French site is on the link I provided. – jdweng Sep 05 '20 at 11:46
  • 1
    The xsd file contains circular references (error thrown by xsd.exe). I have the .xsd files (see OP). Several posts on SO cover this issue and all point to other ways of making your own classes, generating from XML examples, ... That is not part of the questions I have posted however. Do you have any insights on the questions at the bottom of my description? – Starceaker Sep 05 '20 at 13:32
  • 1
    The easiest way of not deserialize an object is remove from the classes or make property private so it is ignored. What you posted looks like a sample xml file for testing and not real. A real xml would have actual values for attributes Ref and Name. – jdweng Sep 05 '20 at 13:47
  • Their example is also in the description: https://filebin.net/3imdjmvxdj37o4ku/Exemple_CatalogueXFT.xml?t=n2qtmi7d – Starceaker Sep 05 '20 at 20:17
  • I'm getting warnings because I do not hve access to the schema <"http://www.exchangefortravel.org/xft/current"> which is the default schema in the link you provided. tyepType is defined in the schema. I n VS menu : Project : Add New Item : Xml File. Then paste xml into view. Error will show in the Error List like any compiler errors. – jdweng Sep 05 '20 at 21:44
  • The Entity Model in my link says following : ANY element from ANY namespace OTHER than <'http://www.exchangefortravel.org/xft/current' > You xml has the namespace xsi as current. – jdweng Sep 05 '20 at 22:08
  • I have been able to generate the classes by using the Liquid Studio suggestion from this post: https://stackoverflow.com/questions/2388844/xsd-exe-and-circular-group-references. It seems like the only way to achieve what I see in the examples is to make 1 class where you have ID + Name properties and 1 class where you only have the Ref property. – Starceaker Sep 06 '20 at 16:25
  • I do not have enough info to give a good solution. I do not see any circular references. I just do not have the complete schema (all the files) to determine the issue. A complex schema like the XFT has many branches and most people do not need all the branches. So you have to be careful how do include the branches you need so you do not get duplication and circular references. If you do get circular references you have to edit the schema. Schema usually have user defined types. You schema looks like it is based on XFT but has unique elements. – jdweng Sep 06 '20 at 21:57

0 Answers0