开发者

Use XSD generated class to deserialize XML in C#?

开发者 https://www.devze.com 2023-03-20 03:01 出处:网络
I created a class from a XML file using the xsd tool that comes with the .NET framework. I included it into my solution. The created class contains many classes and since I am new to this topic I wond

I created a class from a XML file using the xsd tool that comes with the .NET framework. I included it into my solution. The created class contains many classes and since I am new to this topic I wonder which class to use?

StreamReader str = new StreamReader(@"c:\t.xml");
XmlSerializer xSerializer = new XmlSerializer(typeof(SomeClassFromCreatedClass));
SomeClassFromCreatedClassexporis = (SomeClassFromCreatedClass)xSerializer.Deserialize(str);

Problem is, it says Error in XML-Document (2,2) on the 3rd line. So in general, which of the created classes shoul开发者_开发百科d I use? What could be the problem?

Thanks :-)


Sounds like the Xml schema defines many different types (either seperate elements or more likely, nested complex types). Xsd will generate a seperate class for each nested complex type... so you need to ascertain which type is the root of the hierarchy. This would be the type you need to deserialize :)

On a seperate note.. xsd.exe is extremely limited, far better to use something like Xsd2Code :)


The problem is with the error reporting of the XML deserializer.

You will find the real error if you drill down all the inner exceptions, which is quite painful.

0

精彩评论

暂无评论...
验证码 换一张
取 消