0

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?

1
  • 1
    [XmlArray("VectorElement")] Commented Sep 25, 2019 at 18:32

1 Answer 1

1

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;
            }
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this, but when I call deserializer.Deserialize<TMMAI>(response); to do the deserialization, I get an exception stating that System.MissingMethodException: 'No parameterless constructor defined for this object.'. I tried adding a parameterless constructor to the TMMAI class, but I'm still getting the same error. Any ideas?
Just add a default constructor in this class. Updated the code above.
That's exactly what I had added too, but it's still giving the same error for some reason.
Remove Partial from the class declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.