在c#中使用xsd gen进行XML反序列化失败
本文关键字:进行 XML 反序列化 失败 gen xsd | 更新日期: 2023-09-27 18:10:37
我正在尝试反序列化XML文件。我使用xsd来生成结构。看起来文件没有被反序列化或其他东西,因为我在调试模式下将鼠标悬停在数据对象上时得到NULL值。对于这个问题的任何帮助都是非常感谢的。我希望能够循环遍历结构中的所有"功能块"。
包含数据的XML看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<shapes>
<FunctionalBlocks>
<FunctionalBlock uid="{446C5C15-FF16-491D-910B-B8F9F4D90960}" name="1FT001">
<props>
<prop name="Typical or Mode" />
<prop name="Function Template Type">MA</prop>
<prop name="Node">01</prop>
<prop name="System (Tag)">1</prop>
<prop name="Instrumentation Identification (Tag)">FT</prop>
<prop name="Sequence Number (Tag)">001</prop>
<prop name="Comment">Production</prop>
<prop name="SAS System">C</prop>
<prop name="EffectList" />
<prop name="Shape Type">FunctionBlock</prop>
</props>
<pos left="6.220472" top="18.425196" right="7.165354" bottom="17.795275" />
<connects>
<In />
<Out />
<Undirected />
</connects>
</FunctionalBlock>
<FunctionalBlock uid="{2C9BB4F3-FC87-4866-8EA8-48076AFDCF29}" name="1HV001">
<props>
<prop name="Typical or Mode" />
<prop name="Function Template Type">SBE</prop>
<prop name="Node">01</prop>
<prop name="System (Tag)">1</prop>
<prop name="Instrumentation Identification (Tag)">HV</prop>
<prop name="Sequence Number (Tag)">001</prop>
<prop name="Comment" />
<prop name="SAS System">C</prop>
<prop name="EffectList" />
<prop name="Shape Type">FunctionBlock</prop>
</props>
<pos left="7.559055" top="18.425196" right="8.503937" bottom="17.795275" />
<connects>
<In />
<Out />
<Undirected />
</connects>
</FunctionalBlock>
</FunctionalBlocks>
</shapes>
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace NCG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlSerializer serializer = new XmlSerializer(typeof(shapesFunctionalBlocksFunctionalBlockPropsProp), new XmlRootAttribute("shapes"));
FileStream fs = new FileStream("C:''Users''User''Desktop''SCD.xml", FileMode.Open);
XmlReader xtr = XmlReader.Create(fs);
shapesFunctionalBlocksFunctionalBlockPropsProp data = (shapesFunctionalBlocksFunctionalBlockPropsProp)serializer.Deserialize(xtr);
xtr.Close();
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//
// This source code was auto-generated by xsd, Version=4.6.81.0.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[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 shapes
{
private shapesFunctionalBlocks[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FunctionalBlocks", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public shapesFunctionalBlocks[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlocks
{
private shapesFunctionalBlocksFunctionalBlock[] functionalBlockField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FunctionalBlock", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public shapesFunctionalBlocksFunctionalBlock[] FunctionalBlock
{
get
{
return this.functionalBlockField;
}
set
{
this.functionalBlockField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlocksFunctionalBlock
{
private shapesFunctionalBlocksFunctionalBlockPropsProp[][] propsField;
private shapesFunctionalBlocksFunctionalBlockPos[] posField;
private shapesFunctionalBlocksFunctionalBlockConnects[] connectsField;
private string uidField;
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("prop", typeof(shapesFunctionalBlocksFunctionalBlockPropsProp), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public shapesFunctionalBlocksFunctionalBlockPropsProp[][] props
{
get
{
return this.propsField;
}
set
{
this.propsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("pos", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public shapesFunctionalBlocksFunctionalBlockPos[] pos
{
get
{
return this.posField;
}
set
{
this.posField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("connects", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public shapesFunctionalBlocksFunctionalBlockConnects[] connects
{
get
{
return this.connectsField;
}
set
{
this.connectsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlocksFunctionalBlockPropsProp
{
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;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlocksFunctionalBlockPos
{
private string leftField;
private string topField;
private string rightField;
private string bottomField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string left
{
get
{
return this.leftField;
}
set
{
this.leftField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string top
{
get
{
return this.topField;
}
set
{
this.topField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string right
{
get
{
return this.rightField;
}
set
{
this.rightField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string bottom
{
get
{
return this.bottomField;
}
set
{
this.bottomField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlocksFunctionalBlockConnects
{
private string inField;
private string outField;
private string undirectedField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string In
{
get
{
return this.inField;
}
set
{
this.inField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Out
{
get
{
return this.outField;
}
set
{
this.outField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Undirected
{
get
{
return this.undirectedField;
}
set
{
this.undirectedField = value;
}
}
}
}
}
我可以通过复制您的xml并执行EDIT -> Paste Special -> Paste xml作为类来完成此工作。你生成的类一定有问题。
使用这个新类定义,您必须确保在更改其名称时更改按钮单击事件以使用新类:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class shapes
{
private shapesFunctionalBlock[] functionalBlocksField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("FunctionalBlock", IsNullable = false)]
public shapesFunctionalBlock[] FunctionalBlocks
{
get
{
return this.functionalBlocksField;
}
set
{
this.functionalBlocksField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlock
{
private shapesFunctionalBlockProp[] propsField;
private shapesFunctionalBlockPos posField;
private shapesFunctionalBlockConnects connectsField;
private string uidField;
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("prop", IsNullable = false)]
public shapesFunctionalBlockProp[] props
{
get
{
return this.propsField;
}
set
{
this.propsField = value;
}
}
/// <remarks/>
public shapesFunctionalBlockPos pos
{
get
{
return this.posField;
}
set
{
this.posField = value;
}
}
/// <remarks/>
public shapesFunctionalBlockConnects connects
{
get
{
return this.connectsField;
}
set
{
this.connectsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string uid
{
get
{
return this.uidField;
}
set
{
this.uidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlockProp
{
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;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlockPos
{
private decimal leftField;
private decimal topField;
private decimal rightField;
private decimal bottomField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal left
{
get
{
return this.leftField;
}
set
{
this.leftField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal top
{
get
{
return this.topField;
}
set
{
this.topField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal right
{
get
{
return this.rightField;
}
set
{
this.rightField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal bottom
{
get
{
return this.bottomField;
}
set
{
this.bottomField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class shapesFunctionalBlockConnects
{
private object inField;
private object outField;
private object undirectedField;
/// <remarks/>
public object In
{
get
{
return this.inField;
}
set
{
this.inField = value;
}
}
/// <remarks/>
public object Out
{
get
{
return this.outField;
}
set
{
this.outField = value;
}
}
/// <remarks/>
public object Undirected
{
get
{
return this.undirectedField;
}
set
{
this.undirectedField = value;
}
}
}
编辑
用上面的类,和反序列化代码稍微修改一下:
XmlSerializer serializer = new XmlSerializer(typeof(shapes), new XmlRootAttribute("shapes"));
FileStream fs = new FileStream("XMLFile1.xml", FileMode.Open);
XmlReader xtr = XmlReader.Create(fs);
shapes data = (shapes)serializer.Deserialize(xtr);
xtr.Close();
data
现在包含一个类型为shapesFunctionalBlock
的数组。您可以像这样访问它们:
shapesFunctionalBlock foo = data.FunctionalBlocks[0];
shapesFunctionalBlock foo2 = data.FunctionalBlocks[1];
//...etc.