你好,我是執行XML序列化 和我得到這個錯誤:「有反映現場‘消息’錯誤」試圖反序列化XML在C#
我想這是因爲該元素不是必須在XML ..但不太確定。
attachig我的代碼
XML代碼:
<DataStructure>
<message type="01">
<dataBytes position="0" description="Response description" typeOfByte="enum">
<enum val="00" description="Message Legal" />
<enum val="01" description="Message not Recognized" />
<enum val="02" description="Message Data Invalid" />
<enum val="03" description="Unused" />
<enum val="04" description="Buffer Full" />
<enum val="05" description="Unused" />
<enum val="06" description="Notification Error" />
<enum val="07" description="One-Way Network" />
<enum val="08" description="Message Length Error" />
<enum val="09" description="Network Unavailable" />
<enum val="0A" description="Node Busy" />
</dataBytes>
<dataBytes position="1" description="Response data" typeOfByte="enum">
<enum val="00" description="Message Legal" />
<enum val="01" description="Message not Recognized" />
<enum val="02" description="Message Data Invalid" />
<enum val="03" description="Unused" />
<enum val="04" description="Buffer Full" />
<enum val="05" description="Unused" />
<enum val="06" description="Notification Error"/>
<enum val="07" description="One-Way Network" />
<enum val="08" description="Message Length Error" />
<enum val="09" description="Network Unavailable" />
<enum val="0A" description="Node Busy" />
</dataBytes>
</message>
<message type="30">
<dataBytes position="0" description="Node Short ID" typeOfByte="hex"> </dataBytes>
<dataBytes position="1" description="Node Short ID" typeOfByte="bits">
<bit position ="0-2" bitval="" description="Notification Period’ "></bit>
<bit position ="3" bitval="" description ="Listening Mode" ></bit>
<bit position ="4-5" typebit="enum" >
<Enum val="00" description="NA" ></Enum>
<Enum val="01" description="Poor" ></Enum>
<Enum val="10" description="Good" ></Enum>
<Enum val="11" description="Strong" ></Enum>
</bit>
<bit poistion ="6-7" description="Reserved"></bit>
</dataBytes>
</message>
</DataStructure>
C#代碼:
private string readFFDDataStructureXML()
{
string OK = ""; //the error is exits
//perform serialazation for the XML
try
{
string baseDirectory = @"C:\Users\brosenfeld\Perforce\brosenfeld_TA7BarakRo_4075\System\Integration\Tools\Panalyzer\FFDProtocol\DataStructure.xml"; //get current directory
using (FileStream stream = new FileStream(baseDirectory, FileMode.Open))
{
XmlSerializer XML = new XmlSerializer(typeof(DataStructurecs));
_XMLFFDData = (DataStructurecs)XML.Deserialize(stream);
}
}
//if there is an error
catch (Exception err)
{
if (err.InnerException == null)
{
OK = err.Message;
}
else
{
OK = err.Message + "\n\n" + err.InnerException;
}
}
return OK;
}
[XmlRoot("DataStructure")]
public class DataStructurecs
{
[XmlElement("message")]
public List<Message> message;
public DataStructurecs()
{
message = new List<Message>();
}
public class Message
{
[XmlAttribute("type")]
public string type;
[XmlElement("dataBytes")]
public List<dataBytes> DataBytes;
public Message()
{
type="";
DataBytes = new List<dataBytes>();
}
public class dataBytes
{
[XmlAttribute("position")]
public string position;
[XmlAttribute("description")]
public string description;
[XmlAttribute("typeOfByte")]
public string typeOfByte;
[XmlElement("bit")]
public Bits bits;
[XmlArrayItem("enum",(typeof (Enum)),IsNullable=trueQ)]
public List<Enum> ENUM;
public dataBytes()
{
position = "";
description = "";
typeOfByte = "";
bits = new Bits();
ENUM = new List<Enum>();
}
public class Enum
{
[XmlAttribute("val")]
public string val;
[XmlAttribute("description")]
public string description;
public Enum()
{
val = "";
description = "";
}
}
public class Bits
{
[XmlAttribute("position")]
public string position;
[XmlAttribute("bitval")]
public string bitval;
[XmlAttribute("description")]
public string description;
[XmlElement("Enum")]
public List<Enum> BitsEnum;
public Bits()
{
position = "";
bitval = "";
description = "";
BitsEnum = new List<Enum>();
}
public class Enum
{
[XmlAttribute("val")]
public string val;
[XmlAttribute("description")]
public string description;
public Enum()
{
val = "";
description = "";
}
}
}
}
}
}
你的XML格式不完整,最後一個'沒有結束標記 –
是否有任何內部異常?可能會告訴你更多信息 –
只複製和粘貼一些XML代碼來理解問題。再次編輯它。 –