我正在尝试反序列化 XML 并显示其元素的值,但出现此错误:
我刚刚从互联网上找到了一些示例,并尝试根据需要编辑代码,但我相信我做得不对。请参阅下面的类和示例 XML 数据。您的意见和建议将不胜感激。
主窗体
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public FileInfo ItemFile
{
get
{
return new FileInfo(@"C:\Data_120702-015842_440.xml");
}
}
void MainFormLoad(object sender, EventArgs e)
{
if (ItemFile.Exists)
{
List<Record> lst = new List<Record>();
XmlSerializer xml = new XmlSerializer(lst.GetType());
using (Stream s = ItemFile.OpenRead())
{
lst = xml.Deserialize(s) as List<Record>;
}
_items.Add(item);
MessageBox.Show(lst[0].Material_Code);
}
}
}
记录
public class Record
{
public string Material_Code;
public string Sub_Brand_Code;
public string Sub_Brand_Text;
public string Pack_Size_Code;
public string Pack_Size_Text;
public string Pack_Type_Code;
public string Pack_Type_Text;
public Record()
{
}
public Record(string Material_Code,
string Sub_Brand_Code,
string Sub_Brand_Text,
string Pack_Size_Code,
string Pack_Size_Text,
string Pack_Type_Code,
string Pack_Type_Text
)
{
this.Material_Code = Material_Code;
this.Sub_Brand_Code = Sub_Brand_Code;
this.Sub_Brand_Text = Sub_Brand_Text;
this.Pack_Size_Code = Pack_Size_Code;
this.Pack_Size_Text = Pack_Size_Text;
this.Pack_Type_Code = Pack_Type_Code;
this.Pack_Type_Text = Pack_Type_Text;
}
}
XML 结构
<?xml version="1.0" encoding="UTF-8"?>
<MntProdHierAttrMDM>
<Record>
<Material_Code>60024517</Material_Code>
<Sub_Brand_Code>SB000416</Sub_Brand_Code>
<Sub_Brand_Text>Zwitsal</Sub_Brand_Text>
<Pack_Size_Code>PS000224</Pack_Size_Code>
<Pack_Size_Text>50ML</Pack_Size_Text>
<Pack_Type_Code>PT000042</Pack_Type_Code>
<Pack_Type_Text>BOTTLE</Pack_Type_Text>
</Record>
</MntProdHierAttrMDM>