Classes generated from XSD does not work with XmlSerializer

880 Views Asked by At

I need to read some XML files that follow the ONIX standard See: http://www.editeur.org/93/Release-3.0-Downloads/

To do this i downloaded the ONIX 3.0 XSD: http://www.editeur.org/files/ONIX%203/ONIX_BookProduct_XSD_schema+codes_Issue_25.zip

Using the downloaded XSD and this command "xsd your.xsd /classes" i created classes that i want to use.

When trying to create a new Xml Serializer like so:

var xmls = new XmlSerializer(typeof(Model.ONIX.editeur.ONIXMessage));

I get and exception

"There was an error reflecting type 'Model.ONIX.editeur.ONIXMessage'."

When i drill down through the inner exceptions i end up with this message:

"{"Member 'Text' cannot be encoded using the XmlText attribute. You may use the XmlText attribute to encode primitives, enumerations, arrays of strings, or arrays of XmlNode."}"

I am not sure what to do, is something wrong with the XSD? Any suggestions?!

Edit

public static List<Model.ONIX.editeur.Product> GetProductsDataFromOnixFile(string onixFileLocation)
    {
        var xmls = new XmlSerializer(typeof(Model.ONIX.editeur.ONIXMessageRefname));

        using (var reader = XmlReader.Create(onixFileLocation))
        {
            if (xmls.CanDeserialize(reader))
            {
                var onixMessage = (Model.ONIX.editeur.ONIXMessage)xmls.Deserialize(reader);
                return onixMessage.Items.OfType<Model.ONIX.editeur.Product>().ToList();
            }

            throw new Exception(string.Format("Cant read the file {0} as Onix", onixFileLocation));
        }
    }
1

There are 1 best solutions below

0
On

I know this question is old but I assume others with specific Onix issues will run into this.

Here is how I got it to work.

In the reference xsd are two includes in the top. Here I copy/pasted the other two files in.

<xs:include schemaLocation="ONIX_BookProduct_CodeLists.xsd" />
<xs:include schemaLocation="ONIX_XHTML_Subset.xsd" />

I.e. these lines are replaced in the file with the corresponding file.

Then I did the

xsd ONIX_BookProduct_3.0_reference.xsd /classes

And then it generates the .cs file. And the only issue I had here was I had to remove a text attribute from all fields that was e.g. List147, but not from the fields that was string. E.g. I had to remove the attribute from generated code like this:

/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public List121 textscript {
    get {
        return this.textscriptField;

but not from attributes like this

/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
    get {
        return this.valueField;