问题描述
我有一个相当详细的 xml 文件.下面是顶级节点(我已经包含了椭圆,因为较低级别的节点都格式正确并正确填充了数据):
I have a rather detailed xml file. Below is the top level nodes (I have included the ellipse as the lower level nodes are all well formed and properly filled with data):
<?xml version="1.0" encoding="UTF-8"?>
<config>
<Models>...</Models>
<Data>...</Data>
</config>
我已经使用 Visual Studio 2008 命令提示符创建了一个 xsd 文件:
I have created an xsd file from using the Visual Studio 2008 command prompt:
xsd sample.xml
这会生成 xsd 文件就好了.然后我使用以下命令从 xsd 自动生成类:
This generates the xsd file just fine. I then auto generate classes from the xsd with the command:
xsd sample.xsd /classes
为了将xml文件反序列化为类对象,我在helper类中使用了read函数:
For the deserialization of the xml file into a class object, I'm using the read function in the helper class:
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);
我收到一个错误,我推断是因为反序列化器正在寻找模型"节点,但相应的类名是作为根节点和模型"节点 (configModels) 的组合生成的.为什么生成的类名是这样的?
I receive an error that I have deduced is because the deserializer is looking for the 'Models' node but the corresponding class name was generated as a combination of the root node and the 'Model' node (configModels). Why are the class names generated like this?
我尝试使用以下方法从顶部节点反序列化:
I tried to deserialize from the top node using:
var helper = new XmlSerializerHelper<config>();
var obj = new config();
obj = helper.Read(filepath);
不幸的是,这会导致一系列错误,如下所示:
Unfortunately, this the results in a slew of errors like the following:
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 自动生成可能做错的地方吗?
Can somebody steer me towards what I might be doing wrong with my xsd auto-generating?
推荐答案
XSD.EXE 是一个好的开始 - 但它远非完美.此外,根据您提供的 XML,XSD.EXE 无法始终确定某事物是对象的单个实例,还是对象的开放式数组.
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.
这似乎是您的两个元素的情况 - Application.Lease
和 Application.CashFlow
.它们在生成的 XSD 文件中是如何定义的?这对你有意义吗?很可能,您必须添加一些提示,例如:
This seems to be the case for your two elements - Application.Lease
and Application.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:
<xs:element name="Lease" minOccurs="0" maxOccurs="1" />
对于可选属性,仅出现 0 次或 1 次.对于 xsd.exe 工具来说,仅基于单个 XML 示例文件,这样的事情真的很难弄清楚.
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.
马克
这篇关于将 XML 反序列化为 XSD 生成的类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!