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 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

Related Questions

Probably this is a naive question, but I didn't find an answer. I'm creating
This is probably a naive question here but I'll ask it anyway. I'm working
This is probably a naive question but, how do I get the length of
This is probably a naive question - but I want to double check to
This is probably a multi-part question. Background: we have a native (c++) library that
This probably has a simple answer, but I must not have had enough coffee
Ok, this probably has a really simple answer, but I've never tried to do
This probably comes across as a very naive question, and I am absolutely assuming
This is probably a very naive Emacs question - I'm new to it. When
It's probably a naive question, But I am feeling kinda lost. I read iOS

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.