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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:52:12+00:00 2026-05-11T20:52:12+00:00

I have an XSD and I have to generate an XML document to send

  • 0

I have an XSD and I have to generate an XML document to send to the customers of the company I work with. The documents I send will be validated against this XSD schema.

What is the best way to create a XML document conforming to a XSD Schema? I mean, I’m searching for best practices and the like. I’m new to this and while “Googling” around here and there, I found people using XmlTextWriter, DataSet.WriteXml, and others.

  1. DataSet.WriteXml seems to not work well for me. This is what I did:

    var ds = new DataSet();
    ds.ReadXmlSchema(schemaFile);
    ds.Tables["TableName"].Rows.Add("", "", 78, true, DateTime.Now);
    ...
    ds.WriteXml("C:\\xml.xml");
    

    I found it generates a node with NewDataSet, and the nodes are not in the proper order.

  2. XmlTextWriter, I find it a bit long to do… but I will if there is no other choice.

What do you think is the best way to do this? Are there other approaches to do it?
I would put the schema here if it wasn’t so long, and if it were relevant to the question.

  • 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-11T20:52:12+00:00Added an answer on May 11, 2026 at 8:52 pm

    The mainstream practice in .NET is to use XML Serialization.

    In your case, I would do this:

    • run the xsd.exe too on .XSD to generate source code for classes (xsd /c)
    • build your app that utilizes those generated classes. Keep in mind you can extend those classes via the “partial classes” technique
    • in code, instantiate an XmlSerializer, and Serialize the class instances.

    Example:

    Given this schema:

    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Foo" nillable="true" type="Foo" />
      <xs:complexType name="Foo">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="Bar" type="xs:string" />
          <xs:element minOccurs="0" maxOccurs="1" name="Baz" type="UntypedArray" />
        </xs:sequence>
      </xs:complexType>
    
    
      <xs:complexType name="UntypedArray">
        <xs:choice minOccurs="1" maxOccurs="unbounded">
          <xs:element name="Type1" type="Type1"                 minOccurs="1" maxOccurs="1"/>
          <xs:any     namespace="##other" processContents="lax" minOccurs="1" maxOccurs="1"/>
        </xs:choice>
      </xs:complexType>
    
    
      <xs:complexType name="Type1" mixed="true">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="Child" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    

    xsd.exe generates this source code:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
    public partial class Foo {
    
        private string barField;
    
        private object[] bazField;
    
        /// <remarks/>
        public string Bar {
            get {
                return this.barField;
            }
            set {
                this.barField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("", typeof(System.Xml.XmlElement), IsNullable=false)]
        [System.Xml.Serialization.XmlArrayItemAttribute(typeof(Type1), IsNullable=false)]
        public object[] Baz {
            get {
                return this.bazField;
            }
            set {
                this.bazField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class Type1 {
    
        private string childField;
    
        private string[] textField;
    
        /// <remarks/>
        public string Child {
            get {
                return this.childField;
            }
            set {
                this.childField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string[] Text {
            get {
                return this.textField;
            }
            set {
                this.textField = value;
            }
        }
    }
    

    In your app you can instantiate a Foo and then serialize, like this:

        Foo foo = new Foo();
        // ...populate foo here...
        var builder = new System.Text.StringBuilder();
        XmlSerializer s = new XmlSerializer(typeof(Foo));
        using ( var writer = System.Xml.XmlWriter.Create(builder))
        {
            s.Serialize(writer, foo, ns);
        }
        string rawXml = builder.ToString();
    

    This example serializes into a string. Of course you can serialize to other XmlWriters, you can write out to a file, to any arbitrary stream, and so on.

    Normally I tweak the serialization to omit the XML declaration, omit the default xml namespaces, and so on. Like this:

        Foo foo = new Foo();
        // ...populate foo here...
        var builder = new System.Text.StringBuilder();
        var settings = new System.Xml.XmlWriterSettings { OmitXmlDeclaration = true, Indent= true };
        var ns = new XmlSerializerNamespaces();
        ns.Add("","");
        XmlSerializer s = new XmlSerializer(typeof(Foo));
        using ( var writer = System.Xml.XmlWriter.Create(builder, settings))
        {
            s.Serialize(writer, foo, ns);
        }
        string rawXml = builder.ToString();
    

    You can also do the reverse – map from an XML document to an in-memory object graph – using the XmlSerializer. Use the Deserialize method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using xsd:schema which will be used to generated desired xml, I have
I have a an XSD that looks like this (roughly) <xs:schema id=Appointment targetNamespace=http://tempuri.org/Record.xsd elementFormDefault=qualified
If you have a Java object and an XML schema (XSD), what is the
I have an XML document containing types from 2 XML schemas. One (theirs.xsd) is
I am trying to generate a schema from this xml file: http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?rettype=xml&retmode=xml&db=nucleotide&id=AB573763 I store
I'm trying to generate an XML document using a class generated by the xsd.exe
I have two XML files with two different XSD schemas and different namespaces. They
Does anyone have any tips for dealing with ConstraintExceptions thrown by XSD datasets? This
I have this in some WSDL: <element name="startDate" type="xsd:dateTime"/> <element name="endDate" type="xsd:dateTime"/> Which results
I have a reasonably complex XML document which I want to flatten down to

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.