简体   繁体   中英

Deserializing XML into a C# Objects

I have the following XML schema that I'm trying to deserialize into a C# objects:

<TMMAI>
     <StringReturn>123456789</StringReturn>
     <VectorReturn>
          <VectorElement>test1</VectorElement>
          <VectorElement>test2</VectorElement>
          <VectorElement>test3</VectorElement>
          <VectorElement>test4</VectorElement>
          <VectorElement>test5</VectorElement>
          <VectorElement>test6</VectorElement>
          <VectorElement>test7</VectorElement>
     </VectorReturn>
</TMMAI>

and the C# objects I want to deserialize into (generated from https://xmltocsharp.azurewebsites.net/ , but pasting in the above XML schema):

    [XmlRoot(ElementName = "VectorReturn")]
    public class VectorReturn
    {
        [XmlElement(ElementName = "VectorElement")]
        public List<string> VectorElement { get; set; }
    }

    [XmlRoot(ElementName = "TMMAI")]
    public class TMMAI
    {
        [XmlElement(ElementName = "VectorReturn")]
        public VectorReturn VectorReturn { get; set; }
        [XmlElement(ElementName= "StringReturn")]
        public string StringReturn { get; set; }
    }

After initiating and using the default XmlDeserializer, VectorElement has a length of 0 with no data inside of it.

I also tried created my own object hierarchy that was the following, instead of using the generation tool:

    [XmlRoot("TMMAI")]
    public class TMMAI
    {
        [XmlElement("StringReturn")]
        public string StringReturn { get; set; }
        [XmlElement("VectorReturn")]
        public VectorReturn VectorReturn { get; set; }
    }

    public class VectorReturn
    {
        [XmlElement("VectorElement")]
        public List<VectorElement> VectorElements { get; set; }
    }

    public class VectorElement
    {
        public string Element { get; set; }
    }

This one got me closer - VectorElements was a size of 7, which was the correct number of <VectorElement> items in the test XML, but the value of each VectorElement.Element was null .

I'm close, but I can't tell what's wrong at this point. A wrong attribute? Incorrect object design? Need to use the XmlArray attribute?

Visual studio 2015 or greater have built-in XML to class converter. It might give you advantage over serialization/deserialization.Goto Edit -> Past special -> XML to Classes.

Below is the generated class using VS2019 -

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class TMMAI
    {

        private uint stringReturnField;

        private string[] vectorReturnField;
        // Default constructor
        public TMMAI() {} 
        /// <remarks/>
        public uint StringReturn
        {
            get
            {
                return this.stringReturnField;
            }
            set
            {
                this.stringReturnField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("VectorElement", IsNullable = false)]
        public string[] VectorReturn
        {
            get
            {
                return this.vectorReturnField;
            }
            set
            {
                this.vectorReturnField = value;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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