Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:59:00+00:00 2026-06-05T13:59:00+00:00

I want to deserialize the xml string into an object of the type class

  • 0

I want to deserialize the xml string into an object of the type class defined as follows

   [Serializable]
    [XmlRoot("rt")]
    public class XMLSessionParameters
    {
        [XmlArrayItem("el")]
        public List<Elements> Elements { get; set; }
    }

    public class Elements
    {
        [XmlAttribute("nm")]
        public string Name { get; set; }

        [XmlAttribute("vl")]
        public string Value { get; set; }
    }

Following is the XML, which I want to deserialize

<rt>
  <el nm="Name" vl="ABCD_test"/>
  <el nm="Dual" vl="AA"/>
  <el nm="Quad" vl="ABCD"/>
  <el nm="YYMMDD" vl="120614"/>
</rt>

And following are the methods which I am using for deserialization of the XML string

public static XMLSessionParameters DeserializeSessionParameters(string xmlString)
    {
        XMLSessionParameters parameters = (XMLSessionParameters)Deserialize(typeof(XMLSessionParameters), xmlString);
        XElement root = XElement.Parse(xmlString);
        List<XElement> fileElements = root.Elements().ToList();
        foreach (XElement fileEle in fileElements)
        {
            string xml = fileEle.ToString();
            Elements ele = (Elements)Deserialize(typeof(Elements), xml);                
            parameters.Elements.Add(ele);
        }
        return parameters;
    }

private static object Deserialize(Type type, string XmlString)
{
    XmlSerializer serializer = new XmlSerializer(type);
    StringReader stringReader = new StringReader(XmlString);
    XmlReader xmlReader = new XmlTextReader(stringReader);
    object serializedObj = serializer.Deserialize(xmlReader);
    return serializedObj;
}

When I pass the above mentioned XML as string and when Deserialize function is invoked, XMLReader object posseses the value as None and my program ends abruptly, without giving any exceptions.
What can be the cause for such a behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:59:01+00:00Added an answer on June 5, 2026 at 1:59 pm

    The xml does not match the classes; you need:

        [XmlElement("el")]
        public List<Elements> Elements { get; set; }
    

    Your usage of [XmlArrayItem] (with an implicit [XmlArray]) would work for the xml:

    <rt>
      <Elements>
        <el nm="Name" vl="ABCD_test"/>
        <el nm="Dual" vl="AA"/>
        <el nm="Quad" vl="ABCD"/>
        <el nm="YYMMDD" vl="120614"/>
      </Elements>
    </rt>
    

    (note the extra Elements wrapper element). If you use [XmlElement] instead, this is removed.
    Also: remove [Serializable]: XmlSerializer doesn’t need that.

    A simple way to check that your attributes are correct: set up some typical objects and serialize them – compare what you get to the xml you need.


    Full example:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    
    static class Program
    {
        static void Main()
        {
            string xmlString = @"<rt>
      <el nm=""Name"" vl=""ABCD_test""/>
      <el nm=""Dual"" vl=""AA""/>
      <el nm=""Quad"" vl=""ABCD""/>
      <el nm=""YYMMDD"" vl=""120614""/>
    </rt>";
            XMLSessionParameters parameters = (XMLSessionParameters)Deserialize(typeof(XMLSessionParameters), xmlString);
            // parameters now has 4 elements, all correctly configured
        }
        private static object Deserialize(Type type, string XmlString)
        {
            XmlSerializer serializer = new XmlSerializer(type);
            StringReader stringReader = new StringReader(XmlString);
            XmlReader xmlReader = new XmlTextReader(stringReader);
            object serializedObj = serializer.Deserialize(xmlReader);
            return serializedObj;
        }
    
    }
    
    [XmlRoot("rt")]
    public class XMLSessionParameters
    {
        [XmlElement("el")]
        public List<Elements> Elements { get; set; }
    }
    
    public class Elements
    {
        [XmlAttribute("nm")]
        public string Name { get; set; }
    
        [XmlAttribute("vl")]
        public string Value { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got an XML input string and want to deserialize it to an object
is it possible to partially (de)/serialize an object from/into a string? class Foo {
How do I serialize a 'Type'? I want to serialize to XML an object
I want to deserialize back my class using System.Xml.Serialization, but i've noticed a strange
want to know why String behaves like value type while using ==. String s1
I'm having trouble getting Jackson to correctly deserialize json into an object when calling
My class definition: [Serializable] public class MyClass { [XmlAttribute(AttributeName = ID)] //Problem is here.
Is it possible to deserialize an XML file to a class in Flex without
I have problem with deserialize document to object using XmlSerializer class. Code my function
I'm deserializing some XML from an old application into an object in my current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.