2

I have this XML file that I would like to parse into an object in C#:

So, I opened this xml file in VS Express 2013 for Desktop and clicked on the XML -> Generate Schema menu option. This generated two XSD schema files:

I then wrote a batch script which uses the xsd.exe tool (comes with .NET SDK) to generate a C# class file from the schema like this:

@ECHO off
set xsdFile="C:\Users\Administrator\Desktop\test\001-0180914-5787994.xsd"
set outDirectory="C:\Users\Administrator\Desktop\test\out"
set xsdExeDir="C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools"
set language="CS"
cd %xsdExeDir%
xsd.exe "%xsdFile%" "C:\Users\Administrator\Desktop\test\001-0180914-57879941.xsd" /c /out:"%outDirectory%" /l:"%language%"
pause

When I ran the above batch script, it generated the following C# class:

Finally, I added this into a test console app project and I tried to generate an object out of my XML file and this auto-generated class like this:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var order = Parse("001-0180914-5787994.xml");
            Console.WriteLine("Success !!!");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        Console.WriteLine("Press any key to exit...");
        Console.Read();
    }

    public static OrderResponseDetailComplete Parse(String XMLFile)
    {
        var settings = new XmlReaderSettings();
        var obj = new OrderResponseDetailComplete();
        var reader = XmlReader.Create(XMLFile, settings);
        var serializer = new XmlSerializer(typeof(OrderResponseDetailComplete));
        obj = (OrderResponseDetailComplete)serializer.Deserialize(reader);
        reader.Close();
        return obj;
    }
}

When test program ran, I am getting this error:

enter image description here

What does this error mean? and how do I fix it?

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • Post the emitted XML code. – pid Sep 23 '14 at 09:50
  • sorry, what do you mean? I thought I've posted everything I have. – Latheesan Sep 23 '14 at 09:52
  • This XML file I have is produced from the ChannelAdvisor API, so it's not being created manually. Therefore, I am not sure how to fix this `xsi-type` error. Do I ommit it from my XML in my parse function? – Latheesan Sep 23 '14 at 10:05
  • I regret you didn't find your answer, but wanted to thank you, as your snippet helped me on converting the XML back into the righteous classes. – Agustin Garzon Mar 30 '16 at 14:45

1 Answers1

0

From the first link (XML) I see an xsi:type on line 83.

Look at "Is it valid to specify xsi:type for an local complexType?" for more information about this error.

As far as I can tell you need to define the OrderLineItemItemResponse type in your XSD.

The problem is in the XML but the solution is in the XSD. You have to define the type in order for it to be legit and pass validation. For example the first XSD defines OrderResponseDetailComplete. Where is the XSD for OrderLineItemItemResponse?

This may be a viable solution, depending on the use case: XML Serialized from xsd.exe generated code using substitution groups is invalid (invalid xsi:type error).

Community
  • 1
  • 1
pid
  • 11,472
  • 6
  • 34
  • 63
  • The XSD was generated by VisualStudio - not manually. How do I get the VS to generate the XSD correctly? – Latheesan Sep 23 '14 at 10:07
  • I've tried various online XML -> XSD generators, none of them seems to be creating the schema for `OrderLineItemItemResponse` - any idea why this is? – Latheesan Sep 23 '14 at 10:15
  • I had a look at the answer; I don't think this will work for me, as the generated CS code does not even have an entry for `OrderLineItemItemResponse`. As you've said, the solution is in the XSD and I am not sure how to manually amend it.. Any idea why VS is not generating this for me? – Latheesan Sep 23 '14 at 10:41
  • I have no idea. You should search for `Complextype` or *nested type* in XSD. The tool you use might be limited or have an option somewhere. – pid Sep 23 '14 at 13:59