Xml反序列化返回null

时间:2016-01-27 06:04:48

标签: c# xml xml-serialization xml-deserialization

我有以下类,其中包含我想要反序列化xml的类的对象。我正在使用以下技术显示null。实际上我想在问题的最后部分反序列化xml定义。为此目的,我使用以下技术可能是我走错了方向请纠正我

[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() { }
}

下面是用于反序列化的C#代码从文件schedule.xml中读取xml xml的样本定义如下

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

这是schedule.xml文件中定义的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>

3 个答案:

答案 0 :(得分:0)

更改您的课程,然后重试。这是您为xml生成的VS代码。我使用你的xml结构测试了它,它的工作原理。但是您可能需要为完整的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;
        }
    }
}

答案 1 :(得分:0)

您的示例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>

可序列化的类:

[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() { }
}

调用反序列化代码:

DeserializeFromXml<Param>(XMLinStringVariable);

用于反序列化的C#代码:

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

修改:错误在于<professor><course>标记都使用相同的属性名称。所以我在序列化类中添加了XmlElementXmlAttribute标记来分叉它们。

答案 2 :(得分:0)

您必须在类中将id和name标记为XML Attribute,以便在desrialzation期间选择值。我还重构了你的XML。看看下面

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() { }
}

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