简体   繁体   English

Xml Deserialize返回null

[英]Xml Deserialize return null

I have the following class which contains the objects of class i want Deserialize the xml. 我有下面的类,其中包含我要反序列化xml的类的对象。 i am using following technique its showing null. 我正在使用以下技术显示为空。 Actually i want to Deserialize the xml define in the last portion of question. 实际上,我想对问题的最后一部分中的xml定义进行反序列化。 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 以下是反序列化的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);
    }

Here this is sample of xml defined in schedule.xml file 这是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>

Change your classes like this and try again. 像这样更改您的课程,然后重试。 This is the VS generated code for your xml. 这是VS为您的xml生成的代码。 I tested this using your xml structure and it works. 我使用您的xml结构进行了测试,并且可以正常工作。 But you may need to regenerate the code again for the complete 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;
        }
    }
}

Your sample 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>

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: 反序列化的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; }
}

Edit : The error lies in the fact that both <professor> and <course> tag use the same attribute names. 编辑 :错误在于<professor><course>标记都使用相同的属性名称。 So I added XmlElement and XmlAttribute tags in the serialization class to bifurcate them. 因此,我在序列化类中添加了XmlElementXmlAttribute标记以对它们进行分叉。

you have to mark id and name as XML Attribute in the class so that values are picked during desrialzation. 您必须在类中将id和name标记为XML Attribute,以便在反序列化期间选择值。 I have also restructured your XML. 我还重组了您的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 新的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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