XML Serializer为我的XML属性值返回null

时间:2014-07-29 15:09:50

标签: c# xml-serialization

我发布此信息希望第二组眼睛能帮助指出我的错误。我想我在这里忽略了一件简单的事情。基本上,当我将xml文件读入内存时,我继续为ExcelName和MV_Variable获取Null值。我有其他值文件,名称好。

enter image description here

enter image description here

<?xml version="1.0" encoding="utf-8" ?>
<ExcelConfig File="C:\shs\Integrations\ExcelQuery\cfg.xlsx">
  <ExcelSheet Name ="ExcelFormat">
     <Query>
        <Parameter ExcelName="Name" MV_Variable="AreaName"/>
        <Parameter ExcelName="Age" MV_Variable="ZoneName"/>
     </Query>
     <Result>
        <Parameter ExcelName ="Name" MV_Variable ="AreaName"/>
        <Parameter ExcelName="City" MV_Variable="AssetName"/>
     </Result>
  </ExcelSheet>
</ExcelConfig>



public class ExcelConfig
{
    [XmlAttribute]
    public string File { get; set; }

    [XmlElement]
    public List<ExcelSheet> ExcelSheet = new List<ExcelSheet>();
}

public class ExcelSheet
{

    [XmlAttribute]
    public string Name { get; set; }

    [XmlElement]
    public List<Parameter> Query = new List<Parameter>();

    [XmlElement]
    public List<Parameter> Result = new List<Parameter>();

}

public class Parameter
{
    [XmlAttribute]
    public string ExcelName { get; set; }

    [XmlAttribute]
    public string MV_Variable { get; set; }

}

1 个答案:

答案 0 :(得分:1)

按如下方式更改ExcelSheet

public class ExcelSheet
{

    [XmlAttribute]
    public string Name { get; set; }

    [XmlArray]
    public List<Parameter> Query = new List<Parameter>();

    [XmlArray]
    public List<Parameter> Result = new List<Parameter>();
}