我正在尝试生成以下XML:
<Tags>
<Tag name="Motor" path="_types_" type="UDT_DEF">
<Property name="Value"/>
<Property name="DataType">2</Property>
<Parameters>
</Parameters>
<Tag name="Alm" path="" type="Folder"/>
<Tag name="Sts" path="" type="Folder"/>
<Tag name="Comm" path="Alm" type="OPC">
<Property name="Value"/>
<Property name="DataType">6</Property>
<Property name="OPCServer">Tag Server</Property>
<Property name="OPCItemPath">ns=1;s=Alm.Comm</Property>
<Alarms>
<Alarm name="Alarm">
<Property name="setpointA">1.0</Property>
</Alarm>
</Alarms>
</Tag>
<Tag name="Current" path="Sts" type="OPC">
<Property name="Value"/>
<Property name="DataType">6</Property>
<Property name="OPCServer">Tag Server</Property>
<Property name="OPCItemPath">ns=1;s=Sts.Current</Property>
</Tag>
</Tag>
</Tags>
我设置了类,所以我有一个包含Tag的标签类,我也有Alarms等的类。它序列化很好,直到我得到标签元素“Comm”,然后它抛出一个错误:
类型'System.InvalidOperationException'的未处理异常 发生在System.Xml.dll中 附加信息:出现错误 反映类型'TagData.Tags'
当我有多个具有相同名称但子元素不同的元素时,如何将我的类序列化为XML?我的想法是自己处理序列化,但我认为有更好,更简单的解决方案。
这是我的C#课程:
/// <summary>
/// Contains all the tags.
/// </summary>
public class Tags
{
[XmlElement("Tags")]
public List<Tag> ignTagList { get; set; }
}
/// <summary>
/// The tag itself.
/// </summary>
[Serializable()]
public class Tag
{
[XmlAttribute("type")]
public string Type;
[XmlAttribute("path")]
public string Path;
[XmlAttribute("name")]
public string Name;
[XmlElement("Property")]
public List<Property> props { get; set; }
[XmlElement("Tag")]
public List<Folder> folders { get; set; }
[XmlElement("Tag")] // #### ERROR OCCURS HERE. IF I RENAME THIS IT WILL SERIALIZE!! ####
public List<OPCTag> opcTags { get; set; }
public Tag()
{
this.props = new List<Property>();
this.folders = new List<Folder>();
this.opcTags = new List<OPCTag>();
}
public Tag(string Type, string Path, string Name)
{
this.props = new List<Property>();
this.folders = new List<Folder>();
this.opcTags = new List<OPCTag>();
this.Type = Type;
this.Path = Path;
this.Name = Name;
}
}
/// <summary>
/// Property data structure.
/// </summary>
[Serializable()]
public class Property
{
[XmlAttribute("name")]
public string Name;
[XmlText]
public string DataTypeValue;
public Property()
{
}
public Property(string Name, string DataTypeValue)
{
this.Name = Name;
this.DataTypeValue = DataTypeValue;
}
}
/// <summary>
/// Folder data structure.
/// </summary>
[Serializable()]
public class Folder
{
[XmlAttribute("type")]
public string Type;
[XmlAttribute("path")]
public string Path;
[XmlAttribute("name")]
public string Name;
public Folder()
{
}
public Folder(string Type, string Path, string Name)
{
this.Type = Type;
this.Path = Path;
this.Name = Name;
}
public Folder(string Path, string Name)
{
this.Type = "Folder";
this.Path = Path;
this.Name = Name;
}
}
/// <summary>
/// OPC tag data structure.
/// </summary>
[Serializable()]
public class OPCTag
{
[XmlAttribute("type")]
public string Type;
[XmlAttribute("path")]
public string Path;
[XmlAttribute("name")]
public string Name;
[XmlIgnore]
public IgnDataType DataType;
[XmlElement("Property")]
public List<Property> props { get; set; }
public OPCTag()
{
this.props = new List<Property>();
}
public OPCTag(string Type, string Path, string Name)
{
this.Type = Type;
this.Path = Path;
this.Name = Name;
this.props = new List<Property>();
}
public OPCTag(string Path, string Name, UDTDataType DataType)
{
this.Type = "OPC";
this.Path = Path;
this.Name = Name;
this.props = new List<Property>();
this.props.Add(new Property("Value", ""));
this.props.Add(new Property("DataType", ((int)DataType).ToString()));
}
}