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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:19:38+00:00 2026-05-25T10:19:38+00:00

I am wondering if there is a way to get XmlSerializer in C# to

  • 0

I am wondering if there is a way to get XmlSerializer in C# to change what it outputs for one property of one object. For example, if I have something like:

public class MyClass
{
    public string prop1{get;}
    public uint prop2{get;}
    public MyClass2 class2{get;}
    public MyClass3 class3{get;}
}

public class MyClass2
{
    public string prop3{get;}
}

public class MyClass3
{
    //Lots of properties that I want to serialize as normal
}

Now in somewhere in my code, I have something like this:

private void SerializeStuff(List<MyClass> list, string path)
{
    XmlSerializer serialize = new XmlSerializer(typeof(List<MyClass>));
    TextWriter writer = new StreamWriter(path);
    serialize.Serialize(writer, list);
    writer.Close();
}

What I want is for the serialization to work as normal, but with prop3 replaced with some other stuff. Example:

<MyClass>
    <prop1>whatever</prop1>
    <prop2>345</prop2>
    <class2>
        <somethingNotProp3>whatever</somethingNotProp3>
        <somethingElseNotProp3>whatever</somethingElseNotProp3>
    </class2>
    <class3>
        ...
    </class3>
</MyClass>

Is there a way to customize XmlSerializer so I don’t have to write the entire Xml file manually, or is there no way to do that?

Edit:
I am pretty sure that the solution could have something to do with implementing ISerializable‘s GetObjectData method; however, I am not exactly sure how to implement it. I tried making MyClass2 inherit from ISerializable and implementing GetObjectData, but nothing changed. prop3 was still output in the XML file.

  • 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-05-25T10:19:39+00:00Added an answer on May 25, 2026 at 10:19 am

    Use the attributes in the System.Xml.Serialization namespace to affect the way that the instance is serialized.

    If you need very specialized serialization for MyClass3 that the attributes cannot handle, then implement the IXmlSerializable interface which will give you complete control over the serialization process (you can even decide on an instance-by-instance basis how to serialize the content/property).

    Based on the comments, it would seem that you want to implement IXmlSerializable, as you want to change the name of the property and the value; the attributes will let you change the name of the property/element/attribute, but not allow you to perform transformations on the value (and I assume you don’t want to corrupt your representation and add an extra property/value for this purpose).

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

Sidebar

Related Questions

I'm wondering if there is a way to get jquery to load one line
Wondering if there is any way to get the lambda expressions that result from
Just wondering if there is any way to get the NS records in C#.
I'm wondering if there is a way to get better information about the location
I was wondering if there is a way to get a list of results
I was wondering if there is a way to get the URL of the
I'm using jQGrid and I'm wondering if there is a way to get the
I was wondering if there was a quick way to get all of an
I was wondering if there was any possible way to get the number (count)
Hi I was wondering if there is any known way to get rid of

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.