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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:27:57+00:00 2026-06-15T23:27:57+00:00

I am trying to serialize/deserialize the following XML code structure: <Person> <Name/> <Age/> <Address>

  • 0

I am trying to serialize/deserialize the following XML code structure:

<Person>
    <Name/>
    <Age/>
    <Address>
        <BuildingNumber/>
        <Street/>
        <Town/>
        <PostCode/>
    </Address>
</Person>

using the classes

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    public Address Address { get; set; }
}

public class Address {
    public string BuildingNumber { get; set; }
    public string Street { get; set; }
    public string Town { get; set; }
    public string PostCode { get; set; }
}

I am implemeting IXmlSerializable on both classes (it’s over-engineered for this specific example, I know, but it is required for a project I am working on which is rather more complicated!), for which serialisation works fine, but deserialisation does not.

The problem arises in the Person.ReadXml() method, which I have written as:

public void ReadXml(XmlReader reader) {
    reader.ReadStartElement();
    if (!reader.IsEmptyElement) {
        Name = reader.ReadElementContentAsString("Name", string.Empty);
        Age = reader.ReadElementContentAsInt("Age", string.Empty);
        Address = (Address)reader.ReadElementContentAs(typeof(Address), null, "Address", string.Empty);
        // Also failed: Address = (Address)reader.ReadElementContentAsObject("Address", string.Empty);
    }
    reader.ReadEndElement();
}

The final line where Address is being initialised is throwing an exception:

ReadElementContentAs() methods cannot be called on an element that has child elements. (XmlException)

Basically, how would I handle elements with child elements using IXmlSerializable?

  • 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-15T23:27:58+00:00Added an answer on June 15, 2026 at 11:27 pm

    I think you can simply use XmlSerializer here:

    XmlSerializer serializer = new XmlSerializer(typeof(Person));
    Person person = (Person)serializer.Deserialize(reader);
    

    Serialization is also very simple:

    serializer.Serialize(stream, person);
    

    If you will need to customize xml (not your current case), then just use xml serialization attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to deserialize the following xml structure into an object... <?xml version=1.0
I'm trying to make a genric code to serialize and deserialize any object that
I am trying to serialize an xml to a class with the following way:
I'm trying to deserialize the following XML document: <?xml version=1.0 encoding=utf-8 ?> <TestPrice> <Price>
I'm trying to deserialize the following XML output: <?xml version=1.0 encoding=ISO-8859-1?> <Foo> <Val>Data1</Val> </Foo>
I have the following XML response I am trying to deserialize using XmlSerializer. When
I am trying to figure out how to serialize/deserialize an XML listing to C#
I am trying to de-serialize the following XML file. <?xml version=1.0 encoding=UTF-8?> <searchResult> <pagination>
I am trying to serialize/deserialize JSON in Android using GSON. I have two classes
I'm trying to use Json.NET to serialize and deserialze a tree structure for jsTree.

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.