C#反序列化XML,在某些带属性的元素上获取空值 [英] C# Deserialization of XML, getting null values on some of Elements with Attributes

查看:46
本文介绍了C#反序列化XML,在某些带属性的元素上获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有元素和属性的XML文件,问题是在反序列化xml之后,一些元素有空值。

I have this XML file with element and attributes, the problem is some of the elements has null values after deserializing the xml.

以及如何访问属性,例如学生的姓名,座位的数字或行/列?

And how to access the attributes like for example the name of the Student, number or row/column of Seat?

这是我的XML:

<?xml version =" 1.0"编码= QUOT; UTF-8英寸standalone =" yes"?>

<?xml-stylesheet type ='text / xsl'href ='Summary.xsl'?>

<摘要xmlns:xsd =" http://www.w3.org/2001/XMLSchema" xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance">

  <课程> BS CompEng< /课程>

  <学生>

  &NBSP; <学生姓名=" John"> Doe< / Student>

  &NBSP; <学生姓名="简"> Salt< / Student>

  &NBSP; <学生姓名=" G.I."> Joe< / Student>

  &NBSP; < Student name =" Jean Claude"> Van Damme< / Student>

  < /学生>

  < Room_Section>

  &NBSP; <座位号=" 1"行= QUOT 1 QUOT; column =" 1" />

  &NBSP; <座位号=" 2"行= QUOT; 2英寸柱= QUOT; 2英寸/>

  &NBSP; <座位号=" 3"行= QUOT; 3英寸柱= QUOT; 3英寸/>

  &NBSP; <座位号="4"行= QUOT; 4英寸column =" 4" />

  &NBSP; <座位号="5"行= QUOT; 5英寸column =" 5" />

  &NBSP; <座位号="6"行= QUOT; 6英寸column =" 6" />

  < / Room_Section>

  <测试>

  &NBSP; <测试名称=" COE 424">

  &NBSP; &NBSP; <时间>下午1点30分< /时间>

  &NBSP; &NBSP; < Day> 23< / day>

  &NBSP; &NBSP; < Pass> 13< / Pass>

  &NBSP; &NBSP; <失败> 0< /失败>

  &NBSP; &NBSP; < Duration> 01:00:00< / Duration>

  &NBSP; &NBSP; < Teacher name =" Claire"> Something< / Teacher>

  &NBSP; < / Test>

  &NBSP; <测试名称=" CS 101">

  &NBSP; &NBSP; <时间>下午4:30< /时间>

  &NBSP; &NBSP; < Day> 15< / day>

  &NBSP; &NBSP; <传递> 1< /传递>

  &NBSP; &NBSP; <失败> 12< /失败>

  &NBSP; &NBSP; < Duration> 01:30:00< / Duration>

  &NBSP; &NBSP; <教师姓名=" Arnold"> BodyBuilder< /老师>

  &NBSP; < / Test>

  < / Tests>

  < OverallResult>已通过< / OverallResult>

< /摘要>

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type='text/xsl' href='Summary.xsl'?>
<Summary xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Course>BS CompEng</Course>
  <Students>
    <Student name="John">Doe</Student>
    <Student name="Jane">Salt</Student>
    <Student name="G.I.">Joe</Student>
    <Student name="Jean Claude">Van Damme</Student>
  </Students>
  <Room_Section>
    <Seat number="1" row="1" column="1"/>
    <Seat number="2" row="2" column="2" />
    <Seat number="3" row="3" column="3" />
    <Seat number="4" row="4" column="4"/>
    <Seat number="5" row="5" column="5"/>
    <Seat number="6" row="6" column="6"/>
  </Room_Section>
  <Tests>
    <Test name="COE 424">
      <Time>1:30pm</Time>
      <Day>23</Day>
      <Pass>13</Pass>
      <Fail>0</Fail>
      <Duration>01:00:00</Duration>
      <Teacher name="Claire">Something</Teacher>
    </Test>
    <Test name="CS 101">
      <Time>4:30pm</Time>
      <Day>15</Day>
      <Pass>1</Pass>
      <Fail>12</Fail>
      <Duration>01:30:00</Duration>
      <Teacher name="Arnold">BodyBuilder</Teacher>
    </Test>
  </Tests>
  <OverallResult>Passed</OverallResult>
</Summary>

代码:

使用System;

using System;

谢谢,

mcbals

推荐答案

尝试下一个类:

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "", IsNullable = false )]
public partial class Summary
{

    private string courseField;

    private SummaryStudent[] studentsField;

    private SummarySeat[] room_SectionField;

    private SummaryTest[] testsField;

    private string overallResultField;

    /// <remarks/>
    public string Course
    {
        get
        {
            return this.courseField;
        }
        set
        {
            this.courseField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute( "Student", IsNullable = false )]
    public SummaryStudent[] Students
    {
        get
        {
            return this.studentsField;
        }
        set
        {
            this.studentsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute( "Seat", IsNullable = false )]
    public SummarySeat[] Room_Section
    {
        get
        {
            return this.room_SectionField;
        }
        set
        {
            this.room_SectionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute( "Test", IsNullable = false )]
    public SummaryTest[] Tests
    {
        get
        {
            return this.testsField;
        }
        set
        {
            this.testsField = value;
        }
    }

    /// <remarks/>
    public string OverallResult
    {
        get
        {
            return this.overallResultField;
        }
        set
        {
            this.overallResultField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SummaryStudent
{

    private string nameField;

    private string valueField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SummarySeat
{

    private byte numberField;

    private byte rowField;

    private byte columnField;

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

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

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

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SummaryTest
{

    private string timeField;

    private byte dayField;

    private byte passField;

    private byte failField;

    private System.DateTime durationField;

    private SummaryTestTeacher teacherField;

    private string nameField;

    /// <remarks/>
    public string Time
    {
        get
        {
            return this.timeField;
        }
        set
        {
            this.timeField = value;
        }
    }

    /// <remarks/>
    public byte Day
    {
        get
        {
            return this.dayField;
        }
        set
        {
            this.dayField = value;
        }
    }

    /// <remarks/>
    public byte Pass
    {
        get
        {
            return this.passField;
        }
        set
        {
            this.passField = value;
        }
    }

    /// <remarks/>
    public byte Fail
    {
        get
        {
            return this.failField;
        }
        set
        {
            this.failField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute( DataType = "time" )]
    public System.DateTime Duration
    {
        get
        {
            return this.durationField;
        }
        set
        {
            this.durationField = value;
        }
    }

    /// <remarks/>
    public SummaryTestTeacher Teacher
    {
        get
        {
            return this.teacherField;
        }
        set
        {
            this.teacherField = value;
        }
    }

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

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SummaryTestTeacher
{

    private string nameField;

    private string valueField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}


 

(使用"编辑","选择性粘贴"菜单在Visual Studio中生成)。

为了获取属性,请阅读上述对象的相应属性。


这篇关于C#反序列化XML,在某些带属性的元素上获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆