简体   繁体   中英

Xml Deserialize return null

I have the following class which contains the objects of class i want Deserialize the xml. i am using following technique its showing null. Actually i want to Deserialize the xml define in the last portion of question. for this purpose i use the following technique may be i am going in wrong way please correct me

[Serializable]
public class Param 
{
    public Professor Professor { get; set; }
    public Course Course { get; set; }
}
public class Professor
{
    public int id;
    public String name;

    public Professor() { }
}
public class Course
{
    public int id;
    public String name;
    public Course() { }
}

Here folowing is the C# Code for Deserialization the xml reading from file schedule.xml sample of xml is define below

 string path = "//schedule.xml";
    XmlRootAttribute xRoot = new XmlRootAttribute();
    xRoot.ElementName = "param";
    xRoot.IsNullable = true;
    XmlSerializer serializer = new XmlSerializer(typeof(Param),xRoot);
    using (StreamReader reader = new StreamReader(path))
    {
    param = (Param)serializer.Deserialize(reader);
    }

Here this is sample of xml defined in schedule.xml file

 <param>
      <professor id='1' name='Novak J.'></professor>
      <Professor id='2' name='Stanek A.'>  </Professor>
      <course id='1' name='Mathematics' biolab='false'> </course>
      <course id='2' name='Biology' biolab='true'>  </course>
 </param>

Change your classes like this and try again. This is the VS generated code for your xml. I tested this using your xml structure and it works. But you may need to regenerate the code again for the complete xml.

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class param
{

    private paramProfessor professorField;

    private paramProfessor1 professorField1;

    private paramCourse[] courseField;

    /// <remarks/>
    public paramProfessor professor
    {
        get
        {
            return this.professorField;
        }
        set
        {
            this.professorField = value;
        }
    }

    /// <remarks/>
    public paramProfessor1 Professor
    {
        get
        {
            return this.professorField1;
        }
        set
        {
            this.professorField1 = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("course")]
    public paramCourse[] course
    {
        get
        {
            return this.courseField;
        }
        set
        {
            this.courseField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class paramProfessor
{

    private byte idField;

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class paramProfessor1
{

    private byte idField;

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class paramCourse
{

    private byte idField;

    private string nameField;

    private bool biolabField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool biolab
    {
        get
        {
            return this.biolabField;
        }
        set
        {
            this.biolabField = value;
        }
    }
}

Your sample XML:

 <param>
      <professor id='1' name='Novak J.'></professor>
      <professor id='2' name='Stanek A.'>  </professor>
      <course id='1' name='Mathematics' biolab='false'> </course>
      <course id='2' name='Biology' biolab='true'>  </course>
 </param>

The serializable class:

[XmlRoot(Namespace = "", ElementName = "param")]
public class Param
{
    [XmlElement("professor")]
    public List<professor> Professor { get; set; }
    [XmlElement("course")]
    public List<course> Course { get; set; }
}
public class professor
{
    [XmlAttribute("id")]
    public int professorid;
    [XmlAttribute("name")]
    public string professorname;

    public professor() { }
}
public class course
{
    [XmlAttribute("id")]
    public int courseid;
    [XmlAttribute("name")]
    public string coursename;

    public course() { }
}

Call deserialization code:

DeserializeFromXml<Param>(XMLinStringVariable);

C# code for deserialization:

public static T DeserializeFromXml<T>(string xml)
{
    try
    {
        T result;
        XmlSerializer ser = new XmlSerializer(typeof(T));
        using (TextReader tr = new StringReader(xml))
        {
            result = (T)ser.Deserialize(tr);
        }
        return result;
    }
    catch { throw; }
}

Edit : The error lies in the fact that both <professor> and <course> tag use the same attribute names. So I added XmlElement and XmlAttribute tags in the serialization class to bifurcate them.

you have to mark id and name as XML Attribute in the class so that values are picked during desrialzation. I have also restructured your XML. Have a look below

static void Main(string[] args)
    {
        string xml = File.ReadAllText(path of the file);
        Param p = DeserializeFromXml<Param>(xml);
    }

    public static T DeserializeFromXml<T>(string xml)
    {
        try
        {
            T result;
            XmlSerializer ser = new XmlSerializer(typeof(T));
            using (TextReader tr = new StringReader(xml))
            {
                result = (T)ser.Deserialize(tr);
            }
            return result;
        }
        catch { throw; }
    }

}

[Serializable]
[XmlRoot(Namespace = "", ElementName = "param")]
public class Param
{
    [XmlArray("professors")]
    public List<professor> Professor { get; set; }
    [XmlArray("courses")]
    public List<course> Course { get; set; }
}
public class professor
{
   [XmlAttribute("id")]
    public int id { get; set; }
   [XmlAttribute("name")]
    public string name { get; set; }



    public professor() { }
}
public class course
{
     [XmlAttribute("id")]
    public int id { get; set; }
       [XmlAttribute("name")]
    public string name { get; set; }

    public course() { }
}

New XML

<param>
<professors>
      <professor id='1' name='Novak J.'></professor>
      <professor id='2' name='Stanek A.'>  </professor>
</professors>
<courses>
      <course id='1' name='Mathematics' biolab='false'> </course>
      <course id='2' name='Biology' biolab='true'>  </course>
</courses>
</param>

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