繁体   English   中英

如何将此xml反序列化为对象

[英]How to deserialize this xml to an object

有人可以帮我反序列化吗? 我使用xsd.exe了尝试,但是该部分未将条目序列化为条目数组。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns3:OverrideEntriesRequest xmlns:ns3="urn:Bol:Main:S1:Public" xmlns="urn:Bol:Main:M1:Public" xmlns:ns2="urn:Bol:Main:Public">
            <ns3:SessionHeader>
                <ns2:AppLogin>user</ns2:AppLogin>
                <ns2:AppPassword>password</ns2:AppPassword>
            </ns3:SessionHeader>
            <ns3:DataCategoryPath>\\Path\\ToData</ns3:DataCategoryPath>
            <ns3:Entries>
                <Entry>
                    <EntryName>Key1</EntryName>
                    <Attributes>
                        <EntryAttribute>
                            <Name>Attr1</Name>
                        </EntryAttribute>
                        <EntryAttribute>
                            <Name>Attr2</Name>
                            <Value>v1</Value>
                        </EntryAttribute>
                        <EntryAttribute>
                            <Name>Attr3</Name>
                            <Value>V2</Value>
                        </EntryAttribute>
                    </Attributes>
                </Entry>
                <Entry>
                    <EntryName>Key1</EntryName>
                    <Attributes>
                        <EntryAttribute>
                            <Name>Attr1</Name>
                            <Value>Val1</Value>
                        </EntryAttribute>
                        <EntryAttribute>
                            <Name>Attr2</Name>
                            <Value>Val2</Value>
                        </EntryAttribute>
                        <EntryAttribute>
                            <Name>Attr3</Name>
                            <Value>Val3</Value>
                        </EntryAttribute>
                        <EntryAttribute>
                            <Name>Attr4</Name>
                            <Value>Val4</Value>
                        </EntryAttribute>
                        <EntryAttribute>
                            <Name>Attr5</Name>
                            <Value>Val5</Value>
                        </EntryAttribute>
                    </Attributes>
                </Entry>
            </ns3:Entries>
        </ns3:OverrideEntriesRequest>
    </soapenv:Body>
</soapenv:Envelope>

使用此类反序列化您的xml

 /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
        public partial class Envelope
        {

            private EnvelopeBody bodyField;

            /// <remarks/>
            public EnvelopeBody Body
            {
                get
                {
                    return this.bodyField;
                }
                set
                {
                    this.bodyField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public partial class EnvelopeBody
        {

            private OverrideEntriesRequest overrideEntriesRequestField;

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Bol:Main:S1:Public")]
            public OverrideEntriesRequest OverrideEntriesRequest
            {
                get
                {
                    return this.overrideEntriesRequestField;
                }
                set
                {
                    this.overrideEntriesRequestField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:S1:Public")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:Bol:Main:S1:Public", IsNullable = false)]
        public partial class OverrideEntriesRequest
        {

            private OverrideEntriesRequestSessionHeader sessionHeaderField;

            private string dataCategoryPathField;

            private Entry[] entriesField;

            /// <remarks/>
            public OverrideEntriesRequestSessionHeader SessionHeader
            {
                get
                {
                    return this.sessionHeaderField;
                }
                set
                {
                    this.sessionHeaderField = value;
                }
            }

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

            /// <remarks/>
            [System.Xml.Serialization.XmlArrayItemAttribute("Entry", Namespace = "urn:Bol:Main:M1:Public", IsNullable = false)]
            public Entry[] Entries
            {
                get
                {
                    return this.entriesField;
                }
                set
                {
                    this.entriesField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:S1:Public")]
        public partial class OverrideEntriesRequestSessionHeader
        {

            private string appLoginField;

            private string appPasswordField;

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Bol:Main:Public")]
            public string AppLogin
            {
                get
                {
                    return this.appLoginField;
                }
                set
                {
                    this.appLoginField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Bol:Main:Public")]
            public string AppPassword
            {
                get
                {
                    return this.appPasswordField;
                }
                set
                {
                    this.appPasswordField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:M1:Public")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:Bol:Main:M1:Public", IsNullable = false)]
        public partial class Entry
        {

            private string entryNameField;

            private EntryEntryAttribute[] attributesField;

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

            /// <remarks/>
            [System.Xml.Serialization.XmlArrayItemAttribute("EntryAttribute", IsNullable = false)]
            public EntryEntryAttribute[] Attributes
            {
                get
                {
                    return this.attributesField;
                }
                set
                {
                    this.attributesField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Bol:Main:M1:Public")]
        public partial class EntryEntryAttribute
        {

            private string nameField;

            private string valueField;

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

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

并使用以下代码反序列化

XmlSerializer deserializer = new XmlSerializer(typeof(Envelope));
                TextReader reader = new StreamReader(@"D:\1.xml");
                object obj = deserializer.Deserialize(reader);
                Envelope XmlData = (Envelope)obj;
                reader.Close();

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM