' XML文档中存在错误(30,10)'从XML元素类型返回差异类型时出错

时间:2016-02-24 11:43:16

标签: c# xml mapping xml-deserialization

我有一个XML文件。我想把它映射到一个对象。其中一个xml元素是字符串,我想在属性的set部分中将此字符串转换为Enum的int值。然后返回这个int值,但是当文件阅读器想要将我的XML文件反序列化为对象时,我得到运行时错误,该错误表示" XML文档中存在错误(30,10)"。

private int processCodeInt;
    [XmlElement("ProcessCode")]
    public int ProcessCode
    {
        get { return this.processCodeInt; }
        set
        {
            var temp = typeof(Common.Transaction.Definition_Transaction.Def_TrxType_Detail)
                .GetMembers().Where(i => i.MemberType == MemberTypes.Field).ToList();
            this.processCodeInt = Convert.ToInt32(temp.Where(u => u.Name == value.ToString()).FirstOrDefault().ToString());
        }
    }

谢谢大家。

以下我留下我的代码:

映射类:

[XmlRoot("Model")]
    public class Model
    {
        [XmlElement("Transaction")]
        public List<Transaction> Transactions;
    }
    public class Transaction
    {
        [XmlElement("Message")]
        public List<Message> Messages;
    }
    public class Message
    {
        [XmlElement("In")]
        public MessageType In { get; set; }
        [XmlElement("Out")]
        public MessageType Out { get; set; }
    }
    public class MessageType
    {
        [XmlElement("ChannelType")]
        public int ChannelType { get; set; }
        [XmlElement("TransactionType")]
        public int TransactionType { get; set; }
        private int processCodeInt;
        [XmlElement("ProcessCode")]
        public int ProcessCode
        {
            get { return this.processCodeInt; }
            set
            {
                var temp = typeof(Common.Transaction.Definition_Transaction.Def_TrxType_Detail)
                    .GetMembers().Where(i => i.MemberType == MemberTypes.Field).ToList();
                temp.RemoveAt(0);
                MemberInfo[] processCodeMemberInfos = temp.ToArray();
                this.processCodeInt = Convert.ToInt32(processCodeMemberInfos.Where(u => u.Name == value.ToString()).FirstOrDefault().ToString());

            }
        }
        [XmlElement("IsExpired")]
        public bool IsExpired { get; set; }
    }

我的XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<Model>
  <Transaction>
    <Message>
      <In>
        <ChannelType>0</ChannelType>
        <TransactionType>1</TransactionType>
        <ProcessCode>Balance</ProcessCode>
        <IsExpired>0</IsExpired>
      </In>
      <Out>
        <ChannelType>1</ChannelType>
        <TransactionType>1</TransactionType>
        <ProcessCode>Balance</ProcessCode>
        <IsExpired>0</IsExpired>
      </Out>
    </Message>
  </Transaction>
</Model>

映射代码:

var TransactionModelsFile = new System.IO.StreamReader(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
            +@"/TransactionModels.xml");
            System.Xml.Serialization.XmlSerializer reader =
            new System.Xml.Serialization.XmlSerializer(typeof(Model));
            overview = (Model)reader.Deserialize(TransactionModelsFile);
            TransactionModelsFile.Close();

1 个答案:

答案 0 :(得分:1)

正如您所看到的,我刚刚添加了entityManagerFactory而不是字符串,因为Enum不是Balance字段的有效值。但是如果你想将它存储为整数,那么我在这种情况下使用支持字段。

int

enter image description here