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

  • Home
  • SEARCH
  • 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 6829641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:31:26+00:00 2026-05-26T22:31:26+00:00

I’m having a problem when serializing a Dictionary containing a list of derived objects.

  • 0

I’m having a problem when serializing a Dictionary containing a list of derived objects. The serialized output contains

<BaseAttributes xsi:type="Turbine" Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd">

where I would like the BaseAttributes to be substituted with Turbine and the xsi:type to be non-existing.

<Turbine Id="1975fe1f-7aa8-4f1d-b768-93ad262800cd">

My code in overall looks like the following. I have a class BaseAttributes from which I derive some classes for example the Turbine class. These classes are stored in a dictionary with a List of BaseAttributes. The dictionary is an implemented serializable dictionary. Below is the code in general.

[XmlInclude(typeof(Turbine)), XmlInclude(typeof(Station)), XmlInclude(typeof(Substation))]
public class BaseAttributes {

  [XmlAttribute("Id")]
  public Guid Id;
}



public class Turbine : BaseAttributes {
  private Element windSpeed;
  public Element WindSpeed {
    get { return windSpeed; }
    set { windSpeed = value; }
  }

  public Turbine(float windSpeed){
    this.windSpeed= new Element(windSpeed.ToString(),"ms");
  }
  //used for xmlserilization
  private Turbine(){}
}



public class CollectionOfBaseAttributes {
  public SerilizableUnitsDictionary<DateTime, List<BaseAttributes>> units;
}

[XmlRoot("dictionary")]
public class SerilizableUnitsDictionary<TKey, TValue>
: Dictionary<TKey, TValue>, IXmlSerializable {

  public System.Xml.Schema.XmlSchema GetSchema() {
    return null;
  }

  public void WriteXml(System.Xml.XmlWriter writer) {

    XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue), new XmlRootAttribute("Units"));

    foreach (TKey key in this.Keys) {
    writer.WriteStartElement("TimeStamp");              
    writer.WriteAttributeString("Value", key.ToString());

    TValue value = this[key];
    foreach (TValue value1 in Values) {             
      valueSerializer.Serialize(writer, value1);    
    }

    writer.WriteEndElement();
  }
}

I don’t use a DataContractor for the serialization, as I will not be deserializing the XML. I “just” want to create the XML file with attributes.

I have tried to use the XmlElementOverrides, but there is probably something that I just don’ understand in the using. Currently I have tried to use it like this:

XmlAttributes attrs = new XmlAttributes();
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Turbine";
attr.Type = typeof(Turbine);
attrs.XmlElements.Add(attr);
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
attrOverrides.Add(typeof(CollectionOfBaseAttributes ), "BaseAttributes", attrs);

XmlSerializer xmlSerializer = new XmlSerializer(typeof(CollectionOfBaseAttributes ),attrOverrides);

But no result from that.

  • 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-26T22:31:27+00:00Added an answer on May 26, 2026 at 10:31 pm

    Ran into it again today and was disapointed SO didn’t have the answer.

    If it’s a list of objects in a field or a property add this on top:

    [XmlArrayItem(Type = typeof(Turbine))]
    [XmlArrayItem(Type = typeof(Station))]
    

    …

    If it’s a single object add:

     [XmlElement(Type = typeof(Turbine))]
     [XmlElement(Type = typeof(Station))]
    
    • 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 currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.