Я пытаюсь десериализовать XML-файл. Я использовал xsd для создания структуры. Похоже, что файл не получает десериализован или что-то, потому что я получаю значения NULL при наведении курсора на объект данных в режиме отладки. Любая помощь с этой проблемой назначается. Я хочу, чтобы иметь возможность прокручивать все «функциональные блоки» в структуре.Ошибка десериализации XML в C# с использованием xsd gen
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;
}
}
}
}
}
Awesome, спасибо! Я не могу использовать foreach-инструкцию для десериализованного объекта. У вас есть какие-то мысли, как сделать это enumarble? – Snovva1
@ Snovva1 см. Редактирование доступа к массиву. – Crowcoder
Да, не было проблем с доступом к данным с помощью цикла. Я пробовал что-то вроде этого: foreach (FunctionalBlock fb in data) .......... не работал, потому что классы не перечислены, я думаю. – Snovva1