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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:45:34+00:00 2026-05-20T13:45:34+00:00

So, I’m trying to create a specific XML format for the consumers of an

  • 0

So, I’m trying to create a specific XML format for the consumers of an API I’m creating. Effectively, you would have a model like so:

public class SampleModel
{
     public ErrorCodeEnum ErrorCode { get; set; }
     public List<Error> LocalizedErrors { get; set; }
     public object AdditionalInformation { get; set; }
}

Now lets say I have a sample model like so:

SampleModel model = new SampleModel()
model.ErrorCode = ErrorCodeEnum.InvalidParameters;
model.LocalizedErrors = new List<Error>
{
    new Error { For = "SomeField", Message = "Your SomeField value is unacceptable." },
    new Error { For = "SomeField2", Message = "Your SomeField2 value is unacceptable." },
};
model.AdditionalInformation = new SomeOtherType { SomeProperty = "Whatever" };

I’d want to make sure that serializes into something like this:

<xml>
<SampleModel>
   <ErrorCode>InvalidParameters</ErrorCode>
   <Errors>
       <Error For="Somefield">Your SomeField value is unacceptable.</Error>
       <Error For="Somefield2">Your SomeField2 value is unacceptable.</Error>
   </Errors>
   <SomeProperty>Whatever</SomeProperty>
</SampleModel>

I’ve solved a fair amount of this problem, but one stickler remains. Basically, right now, it all serializes out like so:

<xml>
<SampleModel>
   <ErrorCode>InvalidParameters</ErrorCode>
   <Errors>
       <Error For="Somefield">Your SomeField value is unacceptable.</Error>
       <Error For="Somefield2">Your SomeField2 value is unacceptable.</Error>
   </Errors>
   <AdditionalInformation>
       <SomeProperty>Whatever</SomeProperty>
   </AdditionalInformation>
</SampleModel>

The default XmlSerializer is outputting the name of the property (in this case AdditionalInformation) around the serialized nodes of it’s value. I’ve been trying multiple ways to get rid of this node. What I’d like to see is basically my dictionary of values to be appended at the 1 child deep level of the XML Root, and get rid of the AdditionalInformation. So if I had a model like so:

SampleModel model = new SampleModel()
model.ErrorCode = ErrorCodeEnum.InvalidParameters;
model.LocalizedErrors = new List<Error>
{
    new Error { For = "SomeField", Message = "Your SomeField value is unacceptable." },
    new Error { For = "SomeField2", Message = "Your SomeField2 value is unacceptable." },
};
model.AdditionalInformation = new AThirdType { Foo = "Bar", Bing = "Baz" };

I’d get the following XML:

<xml>
<SampleModel>
   <ErrorCode>InvalidParameters</ErrorCode>
   <Errors>
       <Error For="Somefield">Your SomeField value is unacceptable.</Error>
       <Error For="Somefield2">Your SomeField2 value is unacceptable.</Error>
   </Errors>
   <Foo>Bar</Foo>
   <Bing>Baz</Bing>
</SampleModel>

Is this possible without going through the effort of making my own XmlSerializer / Deserializer?

  • 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-20T13:45:34+00:00Added an answer on May 20, 2026 at 1:45 pm

    I am afraid that this is not possible without implementing some custom serialization mechanism (like IXmlSerializable) because imagine that you had this XML and you wanted to deserialize it back to a model. The serializer cannot possibly know that those properties belong to the complex object.

    Another strategy that you could adapt is to simply create a view model class which will be adapted to the desired XML result and then use AutoMapper to map and flatten your model to this view model.

    Yet another possibility is to write your custom serialization mechanism using directly XDocument.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have two tables with like below codes: Table: Accounts id | username |
I am trying to understand how to use SyndicationItem to display feed which is

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.