简体   繁体   中英

Error in Deserializing XML file

I'm trying to deserialize an XML and display a value from its element but I'm getting this error:

在此处输入图片说明

I just found some samples from the internet and tried to edit the code according to my need, but I believe I'm not doing it right. Please see below classes and sample XML data. Your comments and suggestions will be highly appreciated.

MainForm

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);
        }
    }
}

Record

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 Structure

<?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>

You need to add the namespace to the specified element in the xml.

Whenever I run into deserialization issues, I let the serializer solve the problem for me; write the code to serialize the object, and open the xml file that the serializer produces.

See http://msdn.microsoft.com/en-us/library/bdxxw552 for an example of code that performs serialization.

[XmlRoot(ElementName = "MntProdHierAttrMDM")]
public class RecordCollection : List<Record>
{
   public RecordCollection() : base(){}
   public RecordCollection(int capacity) : base(capacity){}
}


void MainFormLoad(object sender, EventArgs e)
{
    if (ItemFile.Exists)
    {
        RecordCollection lst = new RecordCollection();
        XmlSerializer xml = new XmlSerializer(typeof(RecordCollection));

        using (Stream s = ItemFile.OpenRead())
        {
            lst = xml.Deserialize(s) as RecordCollection;
        }

        _items.Add(item);

        MessageBox.Show(lst[0].Material_Code);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM