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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:14:54+00:00 2026-05-26T08:14:54+00:00

I might be doing something totally wrong, but i created a simple test schema:

  • 0

I might be doing something totally wrong, but i created a simple test schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="MyRoot">
    <xs:annotation>
        <xs:documentation>Comment describing your root element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:choice>
            <xs:element name="MyChildOne" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:choice>
                        <xs:element name="SubChild" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:choice>
                    <xs:attribute name="SomeAttribute" type="xs:string"/>
                    <xs:attribute name="SomethingElse" type="xs:string"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="MyChildTwo" type="xs:string" maxOccurs="unbounded"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

One root, two children (one optional).

I ran the Xsd2Code from VS2010 and the generated code created two “root” classes (MyRoot and MyChildOne) without creating the expected MyChildTwo. I would have expected a model with MyRoot.MyChildOne…

Here’s the generated code:

using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;


public partial class MyRoot
{

    private List<object> itemsField;

    public MyRoot()
    {
        this.itemsField = new List<object>();
    }

    public List<object> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

public partial class MyRootMyChildOne
{

    private List<object> itemsField;

    private string someAttributeField;

    private string somethingElseField;

    public MyRootMyChildOne()
    {
        this.itemsField = new List<object>();
    }

    public List<object> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }

    public string SomeAttribute
    {
        get
        {
            return this.someAttributeField;
        }
        set
        {
            this.someAttributeField = value;
        }
    }

    public string SomethingElse
    {
        get
        {
            return this.somethingElseField;
        }
        set
        {
            this.somethingElseField = value;
        }
    }
}

I don’t understand how can I serialize this into a valid (schema compliant) XML file…

Thanks for educating me on this

Cos

  • 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-26T08:14:55+00:00Added an answer on May 26, 2026 at 8:14 am

    If you use xsd.exe to generate this class, it gives you the same thing:

    public partial class MyRoot {
    
        private object[] itemsField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("MyChildOne", typeof(MyRootMyChildOne))]
        [System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
        public object[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
    }
    

    Except for the use of the known type declarations:

    [System.Xml.Serialization.XmlElementAttribute("MyChildTwo", typeof(string))]
    

    So it makes sense if you think about it. Because your child type 2 is a string and string is a simple type in XSD you can add instances of System.String to your Items array and then serialize this out using the above code. Each string will be wrapped in a <MyChildTwo/> node.

    UPDATE

    To make this work you create your type and then use XmlSerializer:

    var root = new MyRoot();
    root.Items = new object[2];
    root.Items[0] = new MyRootMyChildOne { Items = new object[1], SomeAttribute = "test", SomethingElse = "test" };
    root.Items[1] = "hello";
    
    var ser = new XmlSerializer(typeof(MyRoot));
    var memoryStream = new MemoryStream();
    var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
    var streamReader = new StreamReader(memoryStream, Encoding.UTF8);
    ser.Serialize(xmlTextWriter, root);
    memoryStream.Position = 0;
    
    string xml = streamReader.ReadToEnd();
    

    This gives us the following XML:

    <MyRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <MyChildOne SomeAttribute="test" SomethingElse="test" />
        <MyChildTwo>hello</MyChildTwo>
    </MyRoot>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I might be doing something completely wrong here but for some reason the values
I'm pretty new to MSBuild, so I might be doing something obviously-wrong, but a
I'm just starting with ASP.Net MVC 2 and might be doing something wrong. I
I might be doing something stupid. But if I have a normal link like:
Just tried to execute a small Lua script, but unfortunately I'm doing something wrong.
I might be doing something wrong, cuz I'm using buildr for not so long,
Be aware that I might be doing this totally wrong, so I apologize in
I'm pretty new still at C# so I might be doing something stupid, but
I'm new to Java Web Services, so I might be doing things wrong. I'm
Okay, so it seems, for some reason (I might be doing it wrong, obviously),

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.