反序列化 XML 序列项 C#.NET
Deserialize XML Sequence Items C#.NET
我没有找到相关主题,或者我没有正确搜索。
如果已经回答了,我很抱歉。
我有以下XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink">
<xs:element name="root" type="rootType" />
<xs:complexType name="rootType">
<xs:sequence>
<xs:element name="innerElem1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="innerElem2" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="innerElem3" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="innerElem1Attr" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
使用 xsd.exe 工具后,它生成了以下 class
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("root", Namespace="", IsNullable=false)]
public partial class rootType {
private rootTypeInnerElem1[] innerElem1Field;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem1", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1[] innerElem1 {
get {
return this.innerElem1Field;
}
set {
this.innerElem1Field = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootTypeInnerElem1 {
private rootTypeInnerElem1InnerElem2InnerElem3[][] innerElem2Field;
private string innerElem1AttrField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("innerElem3", typeof(rootTypeInnerElem1InnerElem2InnerElem3), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public rootTypeInnerElem1InnerElem2InnerElem3[][] innerElem2 {
get {
return this.innerElem2Field;
}
set {
this.innerElem2Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string innerElem1Attr {
get {
return this.innerElem1AttrField;
}
set {
this.innerElem1AttrField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootTypeInnerElem1InnerElem2InnerElem3 {
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
为了能够正确反序列化,我不得不将双 [][] 替换为一个。
现在,当我尝试反序列化以下 XML
<root>
<innerElem1 innerElem1Attr="attr1">
<innerElem2>
<innerElem3 name="name1">value1</innerElem3>
<innerElem3 name="name2">value2</innerElem3>
<innerElem3 name="name3">value3</innerElem3>
</innerElem2>
<innerElem2>
<innerElem3 name="name4">value4</innerElem3>
<innerElem3 name="name5">value5</innerElem3>
<innerElem3 name="name6">value6</innerElem3>
</innerElem2>
</innerElem1>
</root>
我的对象中只有 rootTypeInnerElem1InnerElem2InnerElem3 数组中的最后 3 个值。
我使用了下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
using (XmlReader xr = XmlReader.Create(@"C:\xmlfiletest.xml"))
{
XmlSerializer xrc = new XmlSerializer(typeof(rootType));
var result = (rootType)xrc.Deserialize(xr);
Console.WriteLine("innerElem1: " + result.innerElem1.Count());
foreach (var elem1 in result.innerElem1)
{
Console.WriteLine(" innerElem2: " + elem1.innerElem2.Count());
foreach (var elem2 in elem1.innerElem2)
{
Console.WriteLine(" innerElemValue3: " + elem2.Value);
}
}
}
Console.ReadLine();
}
catch (Exception _ex)
{
Console.WriteLine(_ex.ToString());
}
}
}
}
输出是
值4
值5
值 6
你能告诉我应该如何修改自动生成的 class 以便我的数组中包含所有 6 个值,而不是只有最后 3 个值吗?
非常感谢。
编辑自动生成的 class 如下,现在我有正确的 2 个数组序列,循环两次给了我所有的值。
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("root", Namespace = "", IsNullable = false)]
public partial class rootType
{
private rootTypeInnerElem1[] innerElem1Field;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem1", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1[] innerElem1
{
get
{
return this.innerElem1Field;
}
set
{
this.innerElem1Field = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootTypeInnerElem1
{
private rootTypeInnerElem1InnerElem2[] innerElem2Field;
private string innerElem1AttrField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem2", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1InnerElem2[] innerElem2
{
get
{
return this.innerElem2Field;
}
set
{
this.innerElem2Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string innerElem1Attr
{
get
{
return this.innerElem1AttrField;
}
set
{
this.innerElem1AttrField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootTypeInnerElem1InnerElem2
{
private rootTypeInnerElem1InnerElem2InnerElem3[] innerElem3Field;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem3", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1InnerElem2InnerElem3[] innerElem3
{
get
{
return this.innerElem3Field;
}
set
{
this.innerElem3Field = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootTypeInnerElem1InnerElem2InnerElem3
{
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
我没有找到相关主题,或者我没有正确搜索。 如果已经回答了,我很抱歉。
我有以下XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink">
<xs:element name="root" type="rootType" />
<xs:complexType name="rootType">
<xs:sequence>
<xs:element name="innerElem1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="innerElem2" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="innerElem3" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="innerElem1Attr" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
使用 xsd.exe 工具后,它生成了以下 class
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("root", Namespace="", IsNullable=false)]
public partial class rootType {
private rootTypeInnerElem1[] innerElem1Field;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem1", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1[] innerElem1 {
get {
return this.innerElem1Field;
}
set {
this.innerElem1Field = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootTypeInnerElem1 {
private rootTypeInnerElem1InnerElem2InnerElem3[][] innerElem2Field;
private string innerElem1AttrField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("innerElem3", typeof(rootTypeInnerElem1InnerElem2InnerElem3), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public rootTypeInnerElem1InnerElem2InnerElem3[][] innerElem2 {
get {
return this.innerElem2Field;
}
set {
this.innerElem2Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string innerElem1Attr {
get {
return this.innerElem1AttrField;
}
set {
this.innerElem1AttrField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootTypeInnerElem1InnerElem2InnerElem3 {
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
为了能够正确反序列化,我不得不将双 [][] 替换为一个。 现在,当我尝试反序列化以下 XML
<root>
<innerElem1 innerElem1Attr="attr1">
<innerElem2>
<innerElem3 name="name1">value1</innerElem3>
<innerElem3 name="name2">value2</innerElem3>
<innerElem3 name="name3">value3</innerElem3>
</innerElem2>
<innerElem2>
<innerElem3 name="name4">value4</innerElem3>
<innerElem3 name="name5">value5</innerElem3>
<innerElem3 name="name6">value6</innerElem3>
</innerElem2>
</innerElem1>
</root>
我的对象中只有 rootTypeInnerElem1InnerElem2InnerElem3 数组中的最后 3 个值。
我使用了下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
using (XmlReader xr = XmlReader.Create(@"C:\xmlfiletest.xml"))
{
XmlSerializer xrc = new XmlSerializer(typeof(rootType));
var result = (rootType)xrc.Deserialize(xr);
Console.WriteLine("innerElem1: " + result.innerElem1.Count());
foreach (var elem1 in result.innerElem1)
{
Console.WriteLine(" innerElem2: " + elem1.innerElem2.Count());
foreach (var elem2 in elem1.innerElem2)
{
Console.WriteLine(" innerElemValue3: " + elem2.Value);
}
}
}
Console.ReadLine();
}
catch (Exception _ex)
{
Console.WriteLine(_ex.ToString());
}
}
}
}
输出是 值4 值5 值 6
你能告诉我应该如何修改自动生成的 class 以便我的数组中包含所有 6 个值,而不是只有最后 3 个值吗?
非常感谢。
编辑自动生成的 class 如下,现在我有正确的 2 个数组序列,循环两次给了我所有的值。
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("root", Namespace = "", IsNullable = false)]
public partial class rootType
{
private rootTypeInnerElem1[] innerElem1Field;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem1", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1[] innerElem1
{
get
{
return this.innerElem1Field;
}
set
{
this.innerElem1Field = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootTypeInnerElem1
{
private rootTypeInnerElem1InnerElem2[] innerElem2Field;
private string innerElem1AttrField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem2", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1InnerElem2[] innerElem2
{
get
{
return this.innerElem2Field;
}
set
{
this.innerElem2Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string innerElem1Attr
{
get
{
return this.innerElem1AttrField;
}
set
{
this.innerElem1AttrField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootTypeInnerElem1InnerElem2
{
private rootTypeInnerElem1InnerElem2InnerElem3[] innerElem3Field;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("innerElem3", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public rootTypeInnerElem1InnerElem2InnerElem3[] innerElem3
{
get
{
return this.innerElem3Field;
}
set
{
this.innerElem3Field = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class rootTypeInnerElem1InnerElem2InnerElem3
{
private string nameField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}