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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:28:59+00:00 2026-05-24T16:28:59+00:00

i have generated a cs-classes with the xsd-tool from some xml scheme. The scheme

  • 0

i have generated a cs-classes with the xsd-tool from some xml scheme.
The scheme is a very complex scheme generated from uml-class descriptions.
Now I have written a simple test to ensure the functionality of the generated xsd-classes.
The test uses XmlSerializer to create the xml-file.

In my XSD-scheme there are some obligatory elements (definition with minocc=1 and maxocc=1 by xsd-default).

For example (simplified):

<xs:element name="order">
    <xs:complexType>
       ...
         <xs:element name="orderId" type="string"/>
         <xs:element name="material" type="Material"/>
       ...
    <xs:complexType>

Material is a complex type, which consists of several elements. It is part of order.

These elements should always exists in the resulting xml-file, even if they are empty or contains only empty elements. Now, if I serialize a class instance to an xml-file, only the elements appear where the class-propertys contain a value. If I set orderId the orderId-element appears, but not the material-element because in my class instance it’s null. As a result my xml-file is not valid to the xsd-schema.

Do you have an idea how i can validate the resulting xml-file with my xsd file during serialization? Is there a way to tell XmlSerializer that it should always generate obligatory elements (perhaps with a default value)?
Or is there a way to validate the structure of the class instance that should be serialized?

  • 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-24T16:29:00+00:00Added an answer on May 24, 2026 at 4:29 pm

    You cannot generate automatically elements to fulfill your required rules.
    On your second question you can validate the generated xml with XmlReader and XmlSchema objects.
    A small example would look like this:

            public IList<string> Validate(TextReader reader, XmlSchema schema)
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.Schemas.Add(schema);
                settings.ValidationType = ValidationType.Schema;
    
                List<string> errors = new List<string>();
    
                settings.ValidationEventHandler += (sender, e) =>
                {
                    errors.Add(string.Format("Line {0} at position {1}{2}{3}",
                            e.Exception.LineNumber, e.Exception.LinePosition,
                                        Environment.NewLine, e.Message));
                };
    
    
                XmlReader xmlReader = XmlReader.Create(reader, settings);
                while (xmlReader.Read()) { };
    
                return errors;
            } 
    

    This example takes a TextReader (can be a StreamReader, StringReader etc. ) and an XmlSchema object (you can construct one from the xsd file or whatever – check the documentation for it) and returns a list of validation errors.

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

Sidebar

Related Questions

I am using classes that were generated from an XML schema using the xsd.exe
I have generated classes from xsd and want to serialize the DateTime . My
I am trying to use the tool XSD.EXE to generate some class files from
We have some code that was generated, mostly likely by svcutil from an XML
I generated some classes off of an xsd that I made from a web
I have generated a set of classes using xsd.exe and created an XML document
I have a T4 template that generates classes from an xml file. How can
I have an XSD, and used the xsd.exe tool to create c# classes. In
I have a class that unmarshals xml from a 3rd party source (I have
I have DataContext classes generated from dbml. Once I get data from the database,

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.