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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:12:21+00:00 2026-05-12T14:12:21+00:00

I’ve come up against a bit of a brick wall with Microsoft’s .net XmlSerializer.

  • 0

I’ve come up against a bit of a brick wall with Microsoft’s .net XmlSerializer. I’m trying to deserialize some XML into an object, which is fine if I’m using a single object, but the problem comes when one puts an object into a List and tries to serialize/deserialize that. First up, here’s a sample C# windows console program to illustrate the problem:

http://pastebin.com/m22e6e275

If the class ‘Foo’ is serialized as a root element, things behave fine, and as expected – the JezNamespace xmlns is applied to the root Foo element, and deserialization occurs fine. However if I create a List and serialize that, the XmlSerializer:
– Creates a root element of ArrayOfFoo
– Puts the Foo elements as children of that element
– Sets the xmlns of EVERY child of Foo to the JezNamespace namespace!

I’m OK with the first two, but the third one seems mad… maybe a bug in XmlSerializer? Is there some way I can deal with this behaviour? I don’t want to be specifying my namespace for every child of Foo, I just want to specify it for Foo. If I do that, currently, XmlSerializer doesn’t deserialize the class properly – it just skips over any Foo element with the JezNamespace xmlns set. I have to set ALL the child elements to have that xmlns.

What I’d like to get to is XmlSerializer generating something like:

<ArrayOfFoo>
    <Foo xmlns="http://schemas.datacontract.org/2004/07/JezNamespace">
        <Field1>hello</Field1>
        <Field2>world</Field2>
    </Foo>
    <Foo xmlns="http://schemas.datacontract.org/2004/07/JezNamespace">
        <Field1>aaa</Field1>
        <Field2>bbb</Field2>
    </Foo>
</ArrayOfFoo>

… and then have XmlSerializer be able to deserialize that properly into a List. Any ideas how I can get it doing this?

  • 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-12T14:12:21+00:00Added an answer on May 12, 2026 at 2:12 pm

    Your code has two attributes on Foo that, so far as I can tell, you’ve put there to try to associate a namespace:

     [XmlRootAttribute(Namespace="http://schemas.datacontract.org/2004/07/JezNamespace",
                       IsNullable=false)]
     [XmlTypeAttribute(AnonymousType=true,
                       Namespace="http://schemas.datacontract.org/2004/07/JezNamespace")]
    

    The first one is simply ignored, because in your scenario Foo is not a root element. The second one doesn’t quite do what you probably think it does – the namespace in it is a namespace of an XSD type, not namespace of an element itself.

    To specify name and namespace of the element, you do instead need to use XmlArrayItemAttribute on the List<Foo> property of a parent class (oh, and you will need that parent class):

    public class FooParent
    {
        [XmlArrayItem(ElementName="Foo",
                      Namespace="http://schemas.datacontract.org/2004/07/JezNamespace")]
        public List<Foo> Foos { get; private set; }
    }
    

    This will produce:

    <FooParent>
      <Foos>
        <Foo xmlns="http://schemas.datacontract.org/2004/07/JezNamespace">
          <Field1>hello</Field1>
          <Field2>world</Field2>
        </Foo>
        ...
    

    Alternatively, if you do not want that Foos intermediate element at all, you can replace XmlArrayItem in code above with XmlElement. In this case, the output XML will look like this:

    <FooParent>
      <Foo xmlns="http://schemas.datacontract.org/2004/07/JezNamespace">
        <Field1>hello</Field1>
        <Field2>world</Field2>
      </Foo>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.