反序列化XML文件 [英] Deserialize XML File
问题描述
我花了几个小时试图反序列化和XML文件,但仍然失败。我把它归结为以下工作:
I have spent a few hours trying to deserialize and XML file but still fail. I boiled it down to the following which WORKS:
XML文件:
<实体xmlns:xsi ='http://www.w3.org/ 2001 / XMLSchema-instance'xmlns:xsd ='http://www.w3.org/2001/XMLSchema'>
<实体>
<名称> a< /名称>
< / Entity>
<实体>
<名称> b< /名称>
< / Entity>
< / Entities>
XML File:
<Entities xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<Entity>
<Name>a</Name>
</Entity>
<Entity>
<Name>b</Name>
</Entity>
</Entities>
在此我运行生成类的xsd.exe。这个类有点长,所以我不会在这里列出它,但我列出了一个在底部不起作用的那个。无论如何,这是我如何反序列化XML文件:
XmlSerializer ser = new XmlSerializer(typeof(Entities));
FileStream fs = new FileStream(" Input.xml" ;,FileMode.Open);
$
实体ents =(实体)ser.Deserialize(fs);
On this I run xsd.exe which generates a class. That class is kind of long so I won't list it here but I list the one that doesn't work at the bottom. Anyways, here is how I deserialize the XML file:
XmlSerializer ser = new XmlSerializer(typeof(Entities));
FileStream fs = new FileStream("Input.xml", FileMode.Open);
Entities ents = (Entities)ser.Deserialize(fs);
这很好用。但是,我的XML文件有一些扩展名如下:
< Entities xmlns:xsi ='http://www.w3.org/2001/XMLSchema-instance'xmlns:xsd ='http: //www.w3.org/2001/XMLSchema'>
<实体>
<名称> a< /名称>
<属性>
< Property>
<名称> aa1Name< /名称>
< / Property>
< Property>
<名称> aa2Name< /名称>
< / Property>
< / Properties>
< / Entity>
<实体>
<名称> b< /名称>
< / Entity>
< /实体>
This works fine. However, my XML file has some extension like this:
<Entities xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<Entity>
<Name>a</Name>
<Properties>
<Property>
<Name>aa1Name</Name>
</Property>
<Property>
<Name>aa2Name</Name>
</Property>
</Properties>
</Entity>
<Entity>
<Name>b</Name>
</Entity>
</Entities>
现在,如果我运行与上面相同的代码,我会收到错误:
$
无法生成临时类(result = 1)。
错误CS0030:无法将类型'[Namespace] .EntitiesEntityPropertiesProperty []'转换为'[Namespace]。 EntitiesEntityPropertiesProperty'
$
错误CS0029:无法将类型'[Namespace] .EntitiesEntityPropertiesProperty'隐式转换为'[Namespace] .EntitiesEntityPropertiesProperty []'
Now if I run the same code as above I get the error:
Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type '[Namespace].EntitiesEntityPropertiesProperty[]' to '[Namespace].EntitiesEntityPropertiesProperty'
error CS0029: Cannot implicitly convert type '[Namespace].EntitiesEntityPropertiesProperty' to '[Namespace].EntitiesEntityPropertiesProperty[]'
为什么这不起作用?我只是在XML文件中添加另一个级别并使用xsd.exe重新生成类
Why does this not work? I am just adding another level to the XML file and regenerate the class with xsd.exe
这是xsd.exe生成的类具有"属性"部分的xml文件:
$
命名空间[命名空间] {
使用System.Xml.Serialization;
&NBSP; ///< remarks />
[System.CodeDom.Compiler.GeneratedCodeAttribute(" xsd"," 2.0.50727.3038")]
$
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
$
[System.ComponentModel.DesignerCategoryAttribute(" code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
$
[System.Xml.Serialization.XmlRootAttribute(Namespace ="",IsNullable = false)]
公共部分类实体{
private EntitiesEntity [] itemsField;
///< remarks />
[System.Xml.Serialization.XmlElementAttribute(" Entity",Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]¥b $ b public EntitiesEntity []项目{
获取{
return this.itemsField;
}
设置{
this.itemsField = value;
}
}
}
///< remarks />
[System.CodeDom.Compiler.GeneratedCodeAttribute(" xsd"," 2.0.50727.3038")]
$
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
$
[System.ComponentModel.DesignerCategoryAttribute(" code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
$
公共部分类EntitiesEntity {
$
private string nameField;
private EntitiesEntityPropertiesProperty [] [] propertiesField;
///< remarks />
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串名称{
获取{
return this.nameField;
}
设置{
this.nameField = value;
}
}
///< remarks />
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
$
[System.Xml.Serialization.XmlArrayItemAttribute(" Property",typeof(EntitiesEntityPropertiesProperty),Form = System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = false)] $
&NBSP;&NBSP;&NBSP;&NBSP; public EntitiesEntityPropertiesProperty [] [] Properties {
获取{
return this.propertiesField;
}
设置{
this.propertiesField = value;
}
}
}
///< remarks />
[System.CodeDom.Compiler.GeneratedCodeAttribute(" xsd"," 2.0.50727.3038")]
$
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
$
[System.ComponentModel.DesignerCategoryAttribute(" code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
$
public partial class EntitiesEntityPropertiesProperty {
$
private string nameField;
///< remarks />
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串名称{
获取{
return this.nameField;
}
设置{
this.nameField = value;
}
}
}
}
Here is the class that xsd.exe generated for the xml file that has the Properties section:
namespace [Namespace] {
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Entities {
private EntitiesEntity[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Entity", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public EntitiesEntity[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EntitiesEntity {
private string nameField;
private EntitiesEntityPropertiesProperty[][] propertiesField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("Property", typeof(EntitiesEntityPropertiesProperty), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public EntitiesEntityPropertiesProperty[][] Properties {
get {
return this.propertiesField;
}
set {
this.propertiesField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class EntitiesEntityPropertiesProperty {
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
}
推荐答案
尝试不同的方法:将XML文本复制到剪贴板,然后转到Visual Studio,转到某些代码文件,然后从菜单执行:编辑,选择性粘贴,粘贴XML
作为类。
似乎应该用'[]'替换'[] []'。
这篇关于反序列化XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!