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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:09:06+00:00 2026-06-05T04:09:06+00:00

I have this class: [XmlRoot(menuItem)] public class MenuItem { [XmlAttribute(text)] public string Text {

  • 0

I have this class:

[XmlRoot("menuItem")]
public class MenuItem
{
    [XmlAttribute("text")]
    public string Text { get; set; }

    [XmlAttribute("isLink")]
    public bool IsLink { get; set; }

    [XmlAttribute("url")]
    public string Url { get; set; }

    [XmlArray("items", IsNullable = true)]
    public List<MenuItem> Items { get; set; }
}

Which defines a menu hierarchy. Now, on serializing this class, the output XML for a 3-level menu is:

<menuItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
text="Tools" isLink="false">
  <items>
    <MenuItem text="Market" isLink="false">
      <items>
        <MenuItem text="Market Analyzer" isLink="true" url="/tools/market/analyzer">
          <items xsi:nil="true" />
        </MenuItem>
      </items>
    </MenuItem>
    <MenuItem text="Banking" isLink="false">
      <items>
        <MenuItem text="Purchase" isLink="true" url="/buy?type=good">
          <items xsi:nil="true" />
        </MenuItem>
      </items>
    </MenuItem>
    <MenuItem text="General" isLink="false">
      <items>
        <MenuItem text="Forecasts" isLink="true" url="/wheather-forcasts?city=la">
          <items xsi:nil="true" />
        </MenuItem>
      </items>
    </MenuItem>
  </items>
</menuItem>

So, MenuItem is both the root and the child-element. As the root, it’s serialized as menuItem with proper casing. However, as child elements, it’s capitalization is not correct. How can I make the serializer create menuItem and not MenuItem in the output for child items. Case sensitivity matters to me here.

I tried to put [XmlElement] attribute on the class itself, but got the following error:

Attribute ‘XmlArrayItem’ is not valid on this declaration type. It is
only valid on ‘property, indexer, field, param, return’ declarations.

Also, I don’t want those default namespaces there, and I don’t want the child items to be created as empty elements. The ultimate XML file should be as clean as this XML example:

<menuItem text='Tools' isLink='false'>
  <items>
    <menuItem text='Market' isLink='false'>
      <items>
        <menuItem text='Market Analyzer' isLink='true' url='/tools/market/analyzer' />
      </items>
    </menuItem>
    <menuItem text='Banking' isLink='false'>
      <items>
        <menuItem text='Purchase' isLink='true' url='/buy?type=good' />
      </items>
    </menuItem>
    <menuItem text='General' isLink='false'>
      <items>
        <menuItem text='Forecasts' isLink='true' url='/wheather-forcasts?city=la' />
      </items>
    </menuItem>
  </items>
</menuItem>

What attributes should I use?

  • 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-05T04:09:07+00:00Added an answer on June 5, 2026 at 4:09 am

    Add XmlArrayItemAttribute and take away the IsNullable:

    [XmlArray("items"), XmlArrayItem("menuItem")]
    public List<MenuItem> Items { get; set; }
    

    To get rid of the extra namespaces, you need to use XmlSerializerNamespaces:

    var ns = new XmlSerializerNamespaces();
    ns.Add("","");
    var ser = new XmlSerializer(typeof (MenuItem));
    ser.Serialize(Console.Out, obj, ns);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class... [XmlRoot(config)] public class SourceConfig { public string Description { get;
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
I have this class: public class Friend { private String name; private String location;
I have this class: public class Product { [Required(ErrorMessage = empty name)] public string
I have this class: public class StatInfo { public string contact; public DateTime date;
I have this code: [Serializable] [XmlRoot(ISO_CCY_CODES)] public class IsoCurrencyCodes { public IsoCurrencyCodes() { IsoCodes
i want to have some class like this: [XmlRoot(ElementName = typeof(T).Name + List)] public
I have this class: class Option # ORM's properties def self.get( id ) #
I have this class constructor: public Category(int max){ ... } The thing is, I
I have this class: public class MyClass { public MyClass(){} public void actionA(){ synchronized(MyClass.class){

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.