我有一个比较详细的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文件转换成类对象的反序列化,我用的辅助类的读取功能:
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" />
有一个可选的属性,这只是零个或一个事件再度发生。这样的事情真的很难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.
马克·