問題描述
我有一個相當詳細的 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 生成的類的問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!