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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:04:23+00:00 2026-05-31T08:04:23+00:00

I am using c# and the XmlSerialiser class to create xml from DTO Objects.

  • 0

I am using c# and the XmlSerialiser class to create xml from DTO Objects.

Now I have to generate such xml text:

<Order>
    <OrderNo>123456</OrderNo>
    <Positions>
        <TextPosition>
            <Text>This is Order No 123456</Text>
        </TextPosition>
        <ItemPosition>
            <ItemId>14789</ItemId>
            <ItemName>Product 1</ItemName>
        </ItemPosition>
        </TextPosition>
        <ItemPosition>
            <ItemId>456</ItemId>
            <ItemName>Product 2</ItemName>
        </ItemPosition>
        <TextPosition>
            <Text>Good bye</Text>
        </TextPosition>
        <SumPosition>
            <Value>123.45 USD</Value>
        </SumPosition>
    </Positions>
</Order>

I use Attributes to decorate my classes and everything works fine.
One thing I couldn’t solve yet. I need to generate a tag Positions with different tag TextPosition, ItemPosition, ValuePosition, ... inside.

How do I achive this in c#?

Currently my class Order contains a

[XmlElement("Positions")]
public PositionList Positions { get; set; }

PositionList is a class with

public class PositionList
{
    [XmlElement("Positions")]
    public List<Object> Positions { get; set; }
}

In order to avoid a InvalidOperationException I added

[XmlInclude(typeof(Textposition))]
[XmlInclude(typeof(ItemPosition))]
[XmlInclude(typeof(SumPosition))]

to my Order class.

However, instead of generating

<Positions>
    <TextPosition>...</TextPosition>
</Positions>

the serializer generates:

<Position d4p1:type="TextPosition" xmlns:d4p1="http://www.w3.org/2001/XMLSchema-instance">...</Position>

The thing is, In don’t need to deserialize it my self but provide a XML-file in a very strict format for one customer. Is there a way to achive 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-31T08:04:25+00:00Added an answer on May 31, 2026 at 8:04 am

    There is a way:

    public class Order
    {
       public List<BasePosition> Positions { get; set; }
       public Order() { Positions = new List<BasePosition>(); }
    }
    
    public class BasePosition
    {
    
    }
    
    public class TextPosition : BasePosition
    {
       public string Text { get; set; }
    }
    
    public class ItemPosition : BasePosition
    {
       public int ItemId { get; set; }
       public string ItemName { get; set; }
    }
    
    public class SumPosition : BasePosition
    {
       public string Value { get; set; }
    }
    

    Example serializing it to an XML file:

    Order o = new Order();
    o.Positions.Add(new TextPosition() { Text = "This is Order No 123456" });
    o.Positions.Add(new ItemPosition() { ItemId = 14789, ItemName = "Product 1" });
    o.Positions.Add(new TextPosition());
    o.Positions.Add(new ItemPosition() { ItemId = 456, ItemName = "Product 2" });
    o.Positions.Add(new SumPosition() { Value = "123.45 USB" });
    XmlAttributeOverrides specific_attributes = new XmlAttributeOverrides();
    XmlAttributes attrs = new XmlAttributes();
    attrs.XmlElements.Add(new XmlElementAttribute(typeof(TextPosition)));
    attrs.XmlElements.Add(new XmlElementAttribute(typeof(ItemPosition)));
    attrs.XmlElements.Add(new XmlElementAttribute(typeof(SumPosition)));
    specific_attributes.Add(typeof(Order), "Positions", attrs);
    XmlSerializer ser = new XmlSerializer(typeof(Order), specific_attributes);
    
    using(MemoryStream mem_stream = new MemoryStream())
    {
       ser.Serialize(mem_stream, o);
       using (BinaryWriter bw = new BinaryWriter(new FileStream("orders.xml", FileMode.Create)))
       {
          bw.Write(mem_stream.ToArray());
       }
    }
    

    Result:

    <?xml version="1.0"?>
    <Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <TextPosition>
        <Text>This is Order No 123456</Text>
      </TextPosition>
      <ItemPosition>
        <ItemId>14789</ItemId>
        <ItemName>Product 1</ItemName>
      </ItemPosition>
      <TextPosition />
      <ItemPosition>
        <ItemId>456</ItemId>
        <ItemName>Product 2</ItemName>
      </ItemPosition>
      <SumPosition>
        <Value>123.45 USB</Value>
      </SumPosition>
    </Order>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchy of classes that are serialised to XML using XMLSerialiser .
I am using the XmlSerializer and have the following property in a class public
I have a schema from a third party that I've generated c# objects from
I leveraging the XmlSerializer to convert to/from XML. Here is an example class: [XmlRootAttribute(myClass)]
I have the following xml that's sent to me from a web service. I'm
I have created a C# class file by using a XSD-file as an input.
I am using a class library which represents some of its configuration in .xml.
I have the following xml I'd like to deserialize into a class <?xml version=1.0
I have an object Foo which I serialize to an XML stream. public class
I am trying to create a base class where I can inherit from it

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.