我必须反序列化下面的xml文件。什么应该是这个类?请帮忙。 [英] I have to deserialize the below xml file. What should be the class for this? Please help.

查看:76
本文介绍了我必须反序列化下面的xml文件。什么应该是这个类?请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<validationrule>
  <fieldname>
    <entitytype>02</entitytype>
    <name>PANRangeLow</name>
    <isrequired>True</isrequired>
    <isrequirederrormessage>PAN Range Low is required</isrequirederrormessage>
    <datatype>Number</datatype>
    <datatypeerrormessage>PAN Range Low must be numeric</datatypeerrormessage>
</fieldname>
  <fieldname>
    <entitytype>02</entitytype>
    <name>PANRangeHigh</name>
    <isrequired>True</isrequired>
    <isrequirederrormessage>PAN Range High is required</isrequirederrormessage>
    <datatype>Number</datatype>
    <datatypeerrormessage>PAN Range High must be numeric</datatypeerrormessage>
  </fieldname>
</validationrule>

推荐答案

XMLSerializer XMLReaderSettings 应该给你一个解决方案。

尝试这个提示 - XML序列化和反序列化 [ ^ ]。
XMLSerializer with XMLReaderSettings should give you a solution.
Try this tip - XML Serialization and Deserialization[^].


你可以试试这个。我从这里找到了解决方案 http://stackoverflow.com/questions/4203540/generate- c-sharp-class-from-xml [ ^ ]



You can try this. I found the solution from here http://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml[^]

/// <remarks />
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class validationrule
    {

        private validationruleFieldname[] fieldnameField;

        /// <remarks />
        [System.Xml.Serialization.XmlElementAttribute("fieldname")]
        public validationruleFieldname[] fieldname
        {
            get
            {
                return this.fieldnameField;
            }
            set
            {
                this.fieldnameField = value;
            }
        }
    }

    /// <remarks />
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class validationruleFieldname
    {

        private byte entitytypeField;

        private string nameField;

        private string isrequiredField;

        private string isrequirederrormessageField;

        private string datatypeField;

        private string datatypeerrormessageField;

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

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

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

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

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

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


应该使用XMLSerializer类。



您可以尝试类似



Should use XMLSerializer class .

You can try something like

[Serializable()]
public class FieldName
{
    [System.Xml.Serialization.XmlElement("EntityType")]
    public string EntityType { get; set; }

    [System.Xml.Serialization.XmlElement("Name")]
    public string Name { get; set; }

    ...
}

[Serializable()]
[System.Xml.Serialization.XmlRoot("validationrule")]
public class validationrules
{
    [XmlArray("FieldName")]
    [XmlArrayItem("FieldName", typeof(FieldName))]
    public FieldName[] fieldNames { get; set; }
}

// the below code may be added into your deserialize method
validationrule rules= null;
string path = "xmlpath.xml";

XmlSerializer serializer = new XmlSerializer(typeof(validationrules));

using (XmlReader reader = XmlReader.Create(path))
    {
        rules =  serializer.Deserialize(reader) as validationrules;
    }





希望这有帮助



Hope this helps


这篇关于我必须反序列化下面的xml文件。什么应该是这个类?请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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