XML 反序列化为 XSD 生成的类时出现问题
我有一个相当详细的 xml 文件。下面是顶级节点(我已经包含了椭圆,因为较低级别的节点都格式良好并且正确填充了数据):
<?xml version="1.0" encoding="UTF-8"?>
<config>
<Models>...</Models>
<Data>...</Data>
</config>
我已经使用 Visual Studio 2008 命令提示符创建了一个 xsd 文件:
xsd sample.xml
这会生成 xsd 文件。然后,我使用以下命令从 xsd 自动生成类:
xsd sample.xsd /classes
为了将 xml 文件反序列化为类对象,我在帮助程序类中使用读取函数:
public class XmlSerializerHelper<T>
{
public Type _type;
public XmlSerializerHelper()
{
_type = typeof(T);
}
public void Save(string path, object obj)
{
using (TextWriter textWriter = new StreamWriter(path))
{
XmlSerializer serializer = new XmlSerializer(_type);
serializer.Serialize(textWriter, obj);
}
}
public T Read(string path)
{
T result;
using (TextReader textReader = new StreamReader(path))
{
XmlSerializer deserializer = new XmlSerializer(_type);
result = (T)deserializer.Deserialize(textReader);
}
return result;
}
}
尝试反序列化时:
var helper = new XmlSerializerHelper<configModels>();
var obj = new configModels();
obj = helper.Read(filepath);
我收到一个我推断出的错误是因为反序列化器正在寻找“Models”节点,但相应的类名是作为根节点和“Model”节点(configModels)的组合生成的。为什么类名会这样生成呢?
我尝试使用以下方法从顶部节点进行反序列化:
var helper = new XmlSerializerHelper<config>();
var obj = new config();
obj = helper.Read(filepath);
不幸的是,这会导致一系列错误,如下所示:
System.InvalidOperationException was unhandled by user code
Message="Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Application.Lease[]' to 'Application.Lease'
error CS0030: Cannot convert type 'Application.CashFlow[]' to 'Application.CashFlow'
...ect.
有人可以引导我解决我的 xsd 自动生成可能做错的事情吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
XSD.EXE 是一个好的开始 - 但它还远非完美。另外,根据您提供的 XML,XSD.EXE 无法始终确定某物是对象的单个实例还是开放式对象数组。
这似乎是您的两个元素的情况 -
Application.Lease
和Application.CashFlow
。它们在生成的 XSD 文件中是如何定义的?这对你来说有意义吗?很可能,您必须添加一些提示,例如:对于可选属性,仅出现零次或一次。对于 xsd.exe 工具来说,仅根据单个 XML 示例文件就很难弄清楚类似的事情。
马克
XSD.EXE is a good start - but it's far from perfect. Also, based on the XML you provided, XSD.EXE can't always decide for sure whether something is a single instance of an object, or an open-ended array of objects.
This seems to be the case for your two elements -
Application.Lease
andApplication.CashFlow
. How are they defined in the generated XSD file? Does that make sense to you? Quite possibly, you'd have to add a little hints, such as:for an optional property, that's zero or one occurences only. Things like that are really hard for the xsd.exe tool to figure out based on just a single XML sample file.
Marc
转到生成的类并从 [][] ---> 更改所有内容[]
Go to your generated class and change all from [][] ---> []
xsd.exe 和列表存在问题。您必须进入生成的类并手动将文件编辑为正确的类型。我已改用 Xsd2Code。目前看来还没有这个问题。
There's an issue with xsd.exe and lists. You have to go into the generated class and manually edit the file to the correct type. I've switched to using Xsd2Code. So far it doesn't seem to have this problem.
可能导致此问题的另一个问题是标签之间的 xml 文件内容(即内容)在不应该编码的情况下仍然进行了编码。例如,我的内容中的
标签仍然是
而不是。 xsd 生成器将这些元素转换为模式中的元素,然后将它们错误地标记为无界,因为发现了多个元素。对它们进行取消编码解决了问题并正确生成了类。
Another issue that can cause this problem is that the xml file contents between the tags (meaning the content) is still encoded when it shouldn't be. For example, the
<br>
tags in my content were still<br>
instead of<br />
. The xsd generator turned these into elements in the schema then mislabeled them as unbounded since there was more than one found. Unencoding them fixed the problem and generated the classes correctly.