使用 XSD 生成的类在 C# 中反序列化 XML?
我使用 .NET 框架附带的 xsd 工具从 XML 文件创建了一个类。我将其纳入我的解决方案中。创建的类包含许多类,由于我是这个主题的新手,我想知道该使用哪个类?
StreamReader str = new StreamReader(@"c:\t.xml");
XmlSerializer xSerializer = new XmlSerializer(typeof(SomeClassFromCreatedClass));
SomeClassFromCreatedClassexporis = (SomeClassFromCreatedClass)xSerializer.Deserialize(str);
问题是,它在第三行显示XML-Document (2,2) 中的错误。那么一般来说,我应该使用哪个创建的类?可能是什么问题?
谢谢 :-)
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 should I use? What could be the problem?
Thanks :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来 Xml 架构定义了许多不同的类型(单独的元素或更可能是嵌套的复杂类型)。 Xsd 将为每个嵌套的复杂类型生成一个单独的类...因此您需要确定哪种类型是层次结构的根。这将是您需要反序列化的类型:)
需要注意的是.. xsd.exe 非常有限,最好使用 Xsd2Code :)
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 :)
问题在于 XML 反序列化器的错误报告。
如果你深入挖掘所有内部异常,你就会发现真正的错误,这是相当痛苦的。
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.