如何反序列化此类XML文档:
<?xml version="1.0" encoding="utf-8"?>
<ModelInformation Version="0.9">
<Model>
<Block BlockType="SubSysteme" Name="subSysteme1">
<p Nom="tag">xx</P>
<p Nom="coordonee">xx</P>
<System>
<p Name="source">xx</P>
<Block BlockType="Entree" Name="Entree_1">
<p Name="coordonne"</P>
</Block>
<Block BlockType="Entree" Name="Entree_2">
<p Name="coordonne">xx</P>
</Block>
<Block BlockType="Entree" Name="Sortie_3">
<p Name="coordonne">xx</P>
</Block>
<Block BlockType="SubSysteme" Name="SubSysteme1.01">
<p Name="tag">xx</P>
<p Name="coordonne">xx</P>
<System>
<p Name="source">xx</P>
<Block BlockType="Entree" Name="XXX">
<p Name="coordonne"</P>
</Block>
<Block BlockType="Sortie" Name="XXX">
<p Name="coordonne">xx</P>
</Block>
</System>
</Block>
</System>
</Block
</Model>
</ModelInformation>
可能有几个系统,子系统,块......
我需要检索他们的属性名称。
首先我尝试了这个:
public class Block
{
public Block() { }
/*[XmlAttribute("BlockType")]
public String BlockType { get; set; }*/
[XmlAttribute("Name")]
public String Name { get; set; }
[XmlArrayItem("Block")]
public List<BlockType> BlockTypes = new List<BlockType>();
}
和主要:
Block block = null;
using (var stream = new StreamReader(@"C:\Users\U503124\Desktop\BstSP_v41_01460_14_01420_v41_0\simulink\blockdiagram.xml"))
{
var serialiser = new XmlSerializer(typeof(Block));
subsystem = (Block)serialiser.Deserialize(stream);
Console.WriteLine(block.Name);
}
block.Name为null,我不知道为什么。
标签HTLM中有相同的标签HTLM。如何处理?
答案 0 :(得分:0)
您的XML无效,因此首先要做的就是修复它。
<ModelInformation Version="0.9">
<Model>
<Block BlockType="SubSysteme" Name="subSysteme1">
<P Nom="tag">xx</P>
<P Nom="coordonee">xx</P>
<System>
<P Name="source">xx</P>
<Block BlockType="Entree" Name="Entree_1">
<P Name="coordonne"></P>
</Block>
<Block BlockType="Entree" Name="Entree_2">
<P Name="coordonne">xx</P>
</Block>
<Block BlockType="Entree" Name="Sortie_3">
<P Name="coordonne">xx</P>
</Block>
<Block BlockType="SubSysteme" Name="SubSysteme1.01">
<P Name="tag">xx</P>
<P Name="coordonne">xx</P>
<System>
<P Name="source">xx</P>
<Block BlockType="Entree" Name="XXX">
<P Name="coordonne"></P>
</Block>
<Block BlockType="Sortie" Name="XXX">
<P Name="coordonne">xx</P>
</Block>
</System>
</Block>
</System>
</Block>
</Model>
</ModelInformation>
...然后你可以选择Paste Special - &gt;将XML粘贴为类
你得到类似的东西作为你的班级代表。这可以整理很多。
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true), System.Xml.Serialization.XmlRoot(Namespace = "", IsNullable = false)]
public class ModelInformation
{
private ModelInformationModel _modelField;
private decimal _versionField;
/// <remarks/>
public ModelInformationModel Model
{
get
{
return _modelField;
}
set
{
_modelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public decimal Version
{
get
{
return _versionField;
}
set
{
_versionField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModel
{
private ModelInformationModelBlock _blockField;
/// <remarks/>
public ModelInformationModelBlock Block
{
get
{
return _blockField;
}
set
{
_blockField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlock
{
private ModelInformationModelBlockP[] _pField;
private ModelInformationModelBlockSystem _systemField;
private string _blockTypeField;
private string _nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("P")]
public ModelInformationModelBlockP[] P
{
get
{
return _pField;
}
set
{
_pField = value;
}
}
/// <remarks/>
public ModelInformationModelBlockSystem System
{
get
{
return _systemField;
}
set
{
_systemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string BlockType
{
get
{
return _blockTypeField;
}
set
{
_blockTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get
{
return _nameField;
}
set
{
_nameField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockP
{
private string _nomField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Nom
{
get
{
return _nomField;
}
set
{
_nomField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlText()]
public string Value
{
get
{
return _valueField;
}
set
{
_valueField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystem
{
private ModelInformationModelBlockSystemP _pField;
private ModelInformationModelBlockSystemBlock[] _blockField;
/// <remarks/>
public ModelInformationModelBlockSystemP P
{
get
{
return _pField;
}
set
{
_pField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Block")]
public ModelInformationModelBlockSystemBlock[] Block
{
get
{
return _blockField;
}
set
{
_blockField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystemP
{
private string _nameField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get
{
return _nameField;
}
set
{
_nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlText()]
public string Value
{
get
{
return _valueField;
}
set
{
_valueField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystemBlock
{
private ModelInformationModelBlockSystemBlockP[] _pField;
private ModelInformationModelBlockSystemBlockSystem _systemField;
private string _blockTypeField;
private string _nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("P")]
public ModelInformationModelBlockSystemBlockP[] P
{
get
{
return _pField;
}
set
{
_pField = value;
}
}
/// <remarks/>
public ModelInformationModelBlockSystemBlockSystem System
{
get
{
return _systemField;
}
set
{
_systemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string BlockType
{
get
{
return _blockTypeField;
}
set
{
_blockTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get
{
return _nameField;
}
set
{
_nameField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystemBlockP
{
private string _nameField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get
{
return _nameField;
}
set
{
_nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlText()]
public string Value
{
get
{
return _valueField;
}
set
{
_valueField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystemBlockSystem
{
private ModelInformationModelBlockSystemBlockSystemP _pField;
private ModelInformationModelBlockSystemBlockSystemBlock[] _blockField;
/// <remarks/>
public ModelInformationModelBlockSystemBlockSystemP P
{
get
{
return _pField;
}
set
{
_pField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Block")]
public ModelInformationModelBlockSystemBlockSystemBlock[] Block
{
get
{
return _blockField;
}
set
{
_blockField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystemBlockSystemP
{
private string _nameField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get
{
return _nameField;
}
set
{
_nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlText()]
public string Value
{
get
{
return _valueField;
}
set
{
_valueField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystemBlockSystemBlock
{
private ModelInformationModelBlockSystemBlockSystemBlockP _pField;
private string _blockTypeField;
private string _nameField;
/// <remarks/>
public ModelInformationModelBlockSystemBlockSystemBlockP P
{
get
{
return _pField;
}
set
{
_pField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string BlockType
{
get
{
return _blockTypeField;
}
set
{
_blockTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get
{
return _nameField;
}
set
{
_nameField = value;
}
}
}
/// <remarks/>
[System.Serializable(), System.ComponentModel.DesignerCategory("code"), System.Xml.Serialization.XmlType(AnonymousType = true)]
public class ModelInformationModelBlockSystemBlockSystemBlockP
{
private string _nameField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttribute()]
public string Name
{
get
{
return _nameField;
}
set
{
_nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlText()]
public string Value
{
get
{
return _valueField;
}
set
{
_valueField = value;
}
}
}
然后使用序列化/反序列化。