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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:55:44+00:00 2026-05-11T16:55:44+00:00

This is probably a naive question about XmlReader , but I haven’t turned up

  • 0

This is probably a naive question about XmlReader, but I haven’t turned up an answer in the MSDN docs.

Suppose that I have XSD SchemaTest.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="pageSettings">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="width" type="xs:decimal" default="8.5" minOccurs="0"/>
        <xs:element name="height" type="xs:decimal" default="11" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

well-formed XML document SchemaTest.xml conforming to this schema

<?xml version="1.0" encoding="utf-8" ?>
<pageSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SchemaTest.xsd">
  <width/>
  <height>11</height>
</pageSettings>

and that I try to read this document with an XmlReader as follows.

static void Main(string[] args) {
    decimal width;
    decimal height;

    XmlReaderSettings settings = new XmlReaderSettings();
    settings.IgnoreWhitespace = true;
    settings.Schemas.Add(null, "C:\\Projects\\SchemaTest\\SchemaTest\\SchemaTest.xsd");
    using (XmlReader reader = XmlReader.Create("C:\\Projects\\SchemaTest\\SchemaTest\\SchemaTest.xml", settings)) {
        reader.ReadStartElement();
        if (reader.Name == "width") {
            width = reader.ReadElementContentAsDecimal("width", "");
            // if fail, width = default from schema
        }
        if (reader.Name == "height") {
            height = reader.ReadElementContentAsDecimal("height", "");
            // if fail, height = default from schema
        }
        reader.ReadEndElement();
    }
}

Currently I am receiving a System.FormatException indicating that the content on the element width is not in the correct format. It appears that reader is trying to read the content in the element and is not defaulting to the default value specified in the schema. What is the correct way to handle this?

Further, it is my understanding that for elements, the schema only provides a default value if an element appears with empty content but that it does not provide a default value if the element is missing. Does this imply that there is no way to obtain a default value for optional elements that are missing?

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

    As for working with empty elements, set settings.ValidationType = ValidationType.Schema and you should get the default value as desired.

    Regarding missing elements, those are considered, well, missing 😉 and thus do not have a default value. Theoretically, you could parse the schema (e.g. using the Schema Object Model) to get the default value. But that would be against the spec.

    Have you considered using attributes, like <pageSettings height="55"/>? In that scenario, you should get the default values for missing attributes.

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

Sidebar

Ask A Question

Stats

  • Questions 128k
  • Answers 128k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no way to prevent a user from adding… May 12, 2026 at 5:43 am
  • Editorial Team
    Editorial Team added an answer It most likely is a with statement. Like this: With… May 12, 2026 at 5:43 am
  • Editorial Team
    Editorial Team added an answer You can add video player components (such as WMP) to… May 12, 2026 at 5:42 am

Related Questions

This is probably a naive question about XmlReader , but I haven't turned up
One of the things I'm working on right now has some similarities to a
I have a method that contains an asynchronous call like this: public void MyMethod()
Trying to make a MySQL-based application support MS SQL, I ran into the following
I've been working with some event handling in Javascript and recently decided to migrate

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.