XMLAttribute 不工作

Posted

技术标签:

【中文标题】XMLAttribute 不工作【英文标题】:XMLAttribute is not working 【发布时间】:2017-05-10 19:51:28 【问题描述】:

我正在将我的 xml 反序列化为 C# 类。

<?xml version="1.0" encoding="UTF-8"?>
<Applications>
    <Application Name = "name1" MakerCheckerType="Operational" SanctionCheckNeeded="N" PreExistCheckNeeded="N" >
    <InstrumentTypes>
        <InstrumentType  Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity">
        </InstrumentType>
        <InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name">
        </InstrumentType>
    </InstrumentTypes>
    <ProcessSteps>
        <ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" />
        <ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/>
    </ProcessSteps>
    </Application>
    <Application Name = "name2" MakerCheckerType="Real" SanctionCheckNeeded="Y" PreExistCheckNeeded="Y">
    <InstrumentTypes>
        <InstrumentType  Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity">
        </InstrumentType>
        <InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name">
        </InstrumentType>
    </InstrumentTypes>
    <ProcessSteps>
        <ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" />
        <ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/>
    </ProcessSteps>
    </Application>
</Applications>

类是:

[XmlType("ProcessStep")]
public class IMAProcessStep

    private string name;
    private string vbaFunction;
    private string excelName;

    [XmlAttribute("Name")]
    public string Name
    
        get  return name; 
        set  name = value; 
    

    [XmlAttribute("VBAFunction")]
    public string VBAFunction
    
        get  return vbaFunction; 
        set  vbaFunction = value; 
    

    [XmlAttribute("ExcelName")]
    public string ExcelName
    
        get  return excelName; 
        set  excelName = value; 
    


[XmlType("InstrumentType")]
public class IMAInstrumentType

    [XmlAttribute("Name")]
    public string Name
    
        get;
        set;
    

    [XmlAttribute("Version")]
    public string Version
    
        get;
        set;
    

    [XmlAttribute("PrimaryKeyExcel")]
    public string PrimaryKeyExcel
    
        get;
        set;
    


[XmlType("Application")]
public class IMAApplication

    [XmlAttribute("Name")]
    public string Name  get; set; 

    [XmlAttribute("MakerCheckerType")]
    public string MakerCheckerType  get; set; 

    public bool IsMakerCheckerType
    
        get
        
            if (MakerCheckerType == "Real")
                return true;
            return false;
        
        set
        
            if (value)
                MakerCheckerType = "Real";
            else
                MakerCheckerType = "Operational";
        
    

    [XmlAttribute("SanctionCheckNeeded")]
    public string SanctionCheckNeeded  get; set; 

    [XmlAttribute("PreExistCheckNeeded")]
    public string PreExistCheckNeeded  get; set; 

    public bool IsSanctionCheckNeeded
    
        get
        
            return SanctionCheckNeeded == "Y";
        
        set
        
            SanctionCheckNeeded = value ? "Y" : "N";
        
    

    public bool IsPreExistCheckNeeded
    
        get
        
            if (PreExistCheckNeeded == "Y")
                return true;
            return false;
        
        set
        
            if (value)
                PreExistCheckNeeded = "Y";
            else
                PreExistCheckNeeded = "N";
        
    

    [XmlArray("ProcessSteps")]
    [XmlArrayItem(ElementName = "ProcessStep")]
    public List<IMAInstrumentType> SupportedInstrumentTypes  get; set; 

    [XmlArray("InstrumentTypes")]
    [XmlArrayItem(ElementName = "InstrumentType")]
    public List<IMAProcessStep> ProcessSteps  get; set; 

这是我如何反序列化它...

List<IMAApplication> appConfig = null;

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFilePath);

var xRoot = new XmlRootAttribute();
xRoot.ElementName = "Applications";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(List<IMAApplication>), xRoot);


using (var stream = File.OpenRead(path))
    appConfig = (List<IMAApplication>)serializer.Deserialize(stream);


return appConfig;

IMAApplication 成功反序列化,但 processSteps 和 InstrumentTypes 仅获取它们的 Name 属性值。其余属性为空。

谁能告诉我这里出了什么问题。

【问题讨论】:

【参考方案1】:

尝试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1

    class Program
    
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        
            IMAApplications applications = Deserialize(FILENAME);
        

        static IMAApplications Deserialize(string configFilePath)
        
            IMAApplications appConfig = null;

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFilePath);

            var xRoot = new XmlRootAttribute();
            xRoot.ElementName = "Applications";
            xRoot.IsNullable = true;
            var serializer = new XmlSerializer(typeof(IMAApplications), xRoot);


            using (var stream = File.OpenRead(path))
                appConfig = (IMAApplications)serializer.Deserialize(stream);


            return appConfig;
        
    
    [XmlRoot("ProcessStep")]
    public class IMAProcessStep
    
        private string name;
        private string vbaFunction;
        private string excelName;

        [XmlAttribute("Name")]
        public string Name
        
            get  return name; 
            set  name = value; 
        

        [XmlAttribute("VBAFunction")]
        public string VBAFunction
        
            get  return vbaFunction; 
            set  vbaFunction = value; 
        

        [XmlAttribute("ExcelName")]
        public string ExcelName
        
            get  return excelName; 
            set  excelName = value; 
        
    

    [XmlRoot("InstrumentType")]
    public class IMAInstrumentType
    
        [XmlAttribute("Name")]
        public string Name
        
            get;
            set;
        
        [XmlAttribute("Version")]
        public string Version
        
            get;
            set;
        
        [XmlAttribute("PrimaryKeyExcel")]
        public string PrimaryKeyExcel
        
            get;
            set;
        
    
    [XmlRoot("Applications")]
    public class IMAApplications
    
        [XmlElement("Application")]
        public List<IMAApplication> applications  get; set; 
    

    [XmlRoot("Application")]
    public class IMAApplication
    
        [XmlAttribute("Name")]
        public string Name  get; set; 
        [XmlAttribute("MakerCheckerType")]
        public string MakerCheckerType  get; set; 

        public bool IsMakerCheckerType
        
            get
            
                if (MakerCheckerType == "Real")
                    return true;
                return false;
            
            set
            
                if (value)
                    MakerCheckerType = "Real";
                else
                    MakerCheckerType = "Operational";
            
        

        [XmlAttribute("SanctionCheckNeeded")]
        public string SanctionCheckNeeded  get; set; 
        [XmlAttribute("PreExistCheckNeeded")]
        public string PreExistCheckNeeded  get; set; 

        public bool IsSanctionCheckNeeded
        
            get
            
                return SanctionCheckNeeded == "Y";
            
            set
            
                SanctionCheckNeeded = value ? "Y" : "N";
            
        
        public bool IsPreExistCheckNeeded
        
            get
            
                if (PreExistCheckNeeded == "Y")
                    return true;
                return false;
            
            set
            
                if (value)
                    PreExistCheckNeeded = "Y";
                else
                    PreExistCheckNeeded = "N";
            
        


    [XmlArray("InstrumentTypes")]
    [XmlArrayItem(ElementName = "InstrumentType")]
    public List<IMAInstrumentType> SupportedInstrumentTypes  get; set; 

    [XmlArray("ProcessSteps")]
    [XmlArrayItem(ElementName = "ProcessStep")]
    public List<IMAProcessStep> ProcessSteps  get; set; 
    

【讨论】:

试过这段代码,结果和我的一样……在 ProcessStep 中只有 Name 属性正在填充,而不是其他属性 您切换了 ProcessSteps 和 InstrumentTypes 的最后 6 行代码。 当我注意到 Equity 是在 Process 而不是 Instruments 时,我发现了这个问题。 感谢您的帮助...我在这个问题上卡住了两个多小时

以上是关于XMLAttribute 不工作的主要内容,如果未能解决你的问题,请参考以下文章

C# 不能在 2 个 Xmlnodes 中添加相同的 XmlAttribute

读取 XmlElement 和 XmlAttribute

将 XmlAttribute 解析为复杂类型

XML 反序列化:在单个属性上使用 XmlAttribute 和 XmlText

在 C# visual2005 中使用 XmlAttribute() 检索

XMLSerializer 异常“存在错误反映字段”和“对于非数组类型,您可以使用以下属性:XmlAttribute,..”