0

I am facing some errors while de-serializing an xml response into objects.Trying to get some data from an external API which returns xml response and convert to object. Error: was not expected, my xml below:

<markets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://test.sample.org/schemas/markets.xsd">
<Fund>
<values>
<Date>2020-11-23</Date>
<Number>xxxxxxx</Number>
<Description>test1</Description>
<Duration>30</Duration>
<currentValue>1000000</currentValue>
<isExist>"Y"</isExist>
<Type>Data</Type>
</values>
<values>
<Date>2020-11-23</Date>
<Number>xxxxxxx</Number>
<Description>test2</Description>
<Duration>31</Duration>
<currentValue>20000000</currentValue>
<isExist>"Y"</isExist>
<Type>Data</Type>
</values>
</Fund>
</markets>

Below is my class containing all the xml roots and elements that I tried to construct to form an object class.

 [Serializable]
    [XmlRoot(ElementName = "markets", Namespace = "http://test.sample.org/schemas/markets.xsd", IsNullable = false)]
    public class Markets
    {
        [XmlElement(ElementName = "Fund")]
        public Fund Fund { get; set; }


    }

    [XmlRoot("Fund")]
    public class Fund
    {
        [XmlElement(ElementName = "values")]
        public List<Values> values = new List<Values>();
    }

    [XmlRoot("values")]
    public class Values
    {
        [XmlElement("Date")]
        public DateTime AsOfDate { get; set; }

        [XmlElement("Number")]
        public string Number{ get; set; }

        [XmlElement("Description")]
        public string Descr { get; set; }

        [XmlElement("Duration")]
        public string Duration { get; set; }

        [XmlElement("currentValue")]
        public decimal CurrValue { get; set; }

        [XmlElement("isExist")]
        public string IsExist { get; set; }

        [XmlElement("Type")]
        public string Type { get; set; }
    }

What am I doing wrong here? My xml can't be changed. Please suggest, you help is appreciated.

My De-serializer code:

using (var httpClient = new HttpClient())
            {
                try
                {
                    HttpResponseMessage response = httpClient.GetAsync(apiurl).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        new WebClient().DownloadFile(apiurl, fullPathName);//downloading the xml file to a physical location
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            string xml = File.ReadAllText(fullPathName);

            XmlSerializer serializer = new XmlSerializer(typeof(Markets));
            StringReader rdr = new StringReader(xml);
            var resultingMessage = (Markets)serializer.Deserialize(rdr); //here I get all the objects
Nilanjan
  • 19
  • 8
  • 4
    Your document doesn't declare a namespace but your serialization types do (well, one does). Also, `xsi:noNamespaceSchemaLocation` refers to the location of an actual schema document, not a namespace, and since you probably don't control the sample.org domain, it's not going to find markets.xsd at the location it specifies. – madreflection Nov 23 '20 at 21:17
  • 1
    There is no error with the xml posted. You will get the error if there are any white spaces before the start of the xml string. Often when copying an xml file to a posting white spaces are removed. So you have to verify if the beginning of your file is what is posted. Try copying the xml from the posting and run with your code. – jdweng Nov 23 '20 at 21:22
  • 1
    @madreflection - Thanks for your suggestion, just removing the namespace did the trick, and I was scratching the code for all day. Able to get the data, I will post my de-serializer code also. jdweng - Thanks for your time and suggestion as well. – Nilanjan Nov 23 '20 at 21:26

0 Answers0