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 8706699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:40:40+00:00 2026-06-13T03:40:40+00:00

I want to serialize an xml string into collection of objects, but I can

  • 0

I want to serialize an xml string into collection of objects, but I can get get only first object. If I add more objects in xml, then it errors out. Not sure what I am missing. I tried declaring type as Emp[]

Here are my both “Emp” xml strings

string empsworking = "<?xml version='1.0' encoding='utf-8'?><Emp><EmpInfo><Code>vv</Code><FirstName>van</FirstName><LastName>sa</LastName><Destination>sc</Destination></EmpInfo><EmpInfo><Code>rr</Code><FirstName>ro</FirstName><LastName>sa</LastName><Destination>ph</Destination></EmpInfo></Emp>";

         string empsNotworking = "<?xml version='1.0' encoding='utf-8'?><Emp><EmpInfo><Code>vv</Code><FirstName>van</FirstName><LastName>sa</LastName><Destination>sc</Destination></EmpInfo></Emp>";

My classes look like

[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Emp
{
    /// <remarks/>
   public EmpInfo EmpInfo { get; set; }
    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }

}

/// <remarks/>
[XmlRoot(ElementName = "EmpInfo")] 
public class EmpInfo
{

    /// <remarks/>
    public string Code;

    /// <remarks/>
    public string FirstName;

    /// <remarks/>
    public string LastName;

    /// <remarks/>
    public string Destination;
}

and my code to serialize is

           StringReader stream = null;
       XmlTextReader reader = null;
        Emp empprofile;
       try
       {
           // serialise to object
           XmlSerializer serializer = new XmlSerializer(typeof(Emp));
           stream = new StringReader(emps); // read xml data
           reader = new XmlTextReader(stream);  // create reader
           // covert reader to object
           empprofile = (Emp)serializer.Deserialize(reader);
       }
       catch
       {
           return null;
       }
       finally
       {
           if (stream != null) stream.Close();
           if (reader != null) reader.Close();
       }

I can only read/get object with empworking.
How can get it as collection of “EmpInfo”? Please guide!

  • 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-13T03:40:41+00:00Added an answer on June 13, 2026 at 3:40 am

    Try like this:

    [TestFixture]
    public class SerializeTest
    {
        [Test]
        public void SerializeEmpTest()
        {
            EmpCollection empCollection = new EmpCollection()
                {
                    new EmpInfo() {Code = "1", FirstName = "Anita"}, 
                    new EmpInfo() {Code = "1", FirstName = "Johan"}
                };
            string xmlString = empCollection.GetXmlString();
        }
    }
    
    [XmlType, XmlRoot]
    public class EmpCollection : List<EmpInfo>
    {
    
    }
    
    [XmlType]
    public class EmpInfo
    {
        public string Code;
    
        public string FirstName;
    
    }
    
    public static class Extenstion
    {
        public static string GetXmlString<T>(this T objectToSerialize)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
            StringBuilder stringBuilder = new StringBuilder();
            string xml;
            using (var xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder)))
            {
                xmlSerializer.Serialize(xmlTextWriter, objectToSerialize);
                xml = stringBuilder.ToString();
            }
            return xml;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to Serialize a 60mb file into XML but it gives me System
I want to serialize an object into an xml and I want the filename
I want to deserialize the xml string into an object of the type class
I want to serialize my object to xml and then to a string. public
I want to serialize an object as xml that contains other custom classes. From
I want to serialize objects to text, but I want to preserve the type
is it possible to partially (de)/serialize an object from/into a string? class Foo {
I want to serialize a ReportDocument using XML serialization but in vain, that's my
How do I serialize a 'Type'? I want to serialize to XML an object
I want to serialize a class to xml and store that in a field

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.