Tekin Güllü
Tekin Güllü

Reputation: 363

C# Xml Deserializion error

I am newby .net and xamarin. I am trying develop a xamarin form application. When I try to deserialize my xml I take error.

Error Message is  

at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x0013d] in <6ae4606e5b2b46498c0ae37681c7e745>:0 at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00048] in <6ae4606e5b2b46498c0ae37681c7e745>:0 at System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Xml.Serialization.XmlAttributeOverrides overrides, System.Type[] extraTypes, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00041] in <6ae4606e5b2b46498c0ae37681c7e745>:0 at System.Xml.Serialization.XmlSerializer..ctor (System.Type type) [0x00000] in <6ae4606e5b2b46498c0ae37681c7e745>:0 at Fmkt44.icerik.Deserialize1 (System.String IasReturn) [0x00002] in C:\Users\TEKINHP\source\repos\FimaksApp\Fmkt44\Fmkt44\icerik.xaml.cs:83

[XmlRoot("REPORTLIST")]
[Serializable]
class REPORTLIST
{
    public REPORTLIST()
    {

    }       

    public List<ROW> ROW { get; set; }

}
[Serializable]
class ROW
{
    public ROW()
    {

    }
    public string INSTNUMBER { get; set; }
    public string MATERIAL { get; set; }

}

My xml File is

 <REPORTLIST>
     <ROW>
        <MATERIAL>A</MATERIAL>
        <INSTNUMBER>B</INSTNUMBER>
    </ROW>
 </REPORTLIST>

This is My Deserialize methods

    public static Stream GenerateStreamFromString(string s)
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(s);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }
    REPORTLIST Deserialize1(String MyXml)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(REPORTLIST));
      return (REPORTLIST)serializer.Deserialize(GenerateStreamFromString(MyXml));

    }

Error Occurrs at serializer.Deserialize

Upvotes: 4

Views: 434

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062745

The error message is:

REPORTLIST is inaccessible due to its protection level. Only public types can be processed.

Make the types public. XmlSerializer cannot work on internal types. Also: you don't need the [Serializable] - XmlSerializer does not care.

You will also need [XmlElement] on the collection, to tell it not to add/expect a wrapper element.

Final working version:

[XmlRoot("REPORTLIST")]
public class ReportList
{
    [XmlElement("ROW")]
    public List<Row> Rows { get; } = new List<Row>();    
}
public class Row
{
    [XmlElement("INSTNUMBER")]
    public string InstNumber { get; set; }
    [XmlElement("MATERIAL")]
    public string Material { get; set; }
}

Upvotes: 3

Related Questions