FIX: DataSet Containing Attribute Column and Default Value Generates Exception When AllowDBNull Set to False (320881)



The information in this article applies to:

  • Microsoft XML Classes (included with the .NET Framework 1.0)

This article was previously published under Q320881

SYMPTOMS

When you try to load a DataSet that contains a combination of an attribute column and a default value, and AllowDBNull is set to false, System.Xml.Schema.Compiler generates an XmlSchemaException exception.

For example, if you serialize the following DataSet to XML
dc = dt.Columns.Add("c2", System.Type.GetType("System.String"));
dc.AllowDBNull = false;
dc.DefaultValue = "Hello";
dc.ColumnMapping = MappingType.Attribute;
				
the DataSet generates an XML schema that contains the following definition:
<xs:element name="Table1">
     <xs:complexType>
      <xs:attribute name="c2" type="xs:string" default="Hello" use="required" />
      </xs:complexType>
</xs:element>
				
According to the World Wide Web Consortium (W3C) XML schema specification, the use attribute must be optional or absent if the default attribute is present. Therefore, the schema that the DataSet generates is not valid.

RESOLUTION

To work around this problem, either set the column mapping as an element instead of as an attribute, or do not set the default value.

STATUS

This bug was corrected in Microsoft .NET Framework Class Libraries.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET, create a Visual C# Console Application project. Replace the code in Class1.cs with the following:
    using System;
    using System.Data;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace ConsoleApplication1
    {
       class Class1
       {
          [STAThread]
          static void Main(string[] args)
          {
             try
             {
                DataSet ds = new DataSet();
                DataTable dt = ds.Tables.Add("Table1");
                DataColumn dc;
    
                dc = dt.Columns.Add("c2", System.Type.GetType("System.String"));
                dc.AllowDBNull = false;
                // Workaround 1: Comment the following line to avoid the problem (having no default value).
                dc.DefaultValue = "Hello";
                dc.ColumnMapping = MappingType.Attribute;
                // Workaround 2: Set the ColumnMapping to element as shown below to avoid the problem.
                // dc.ColumnMapping = MappingType.Element;
    
                ds.AcceptChanges();
                ds.WriteXml("dataset.xml", XmlWriteMode.WriteSchema);
    
                DataSet ds2 = new DataSet();
                ds2.ReadXml("dataset.xml");
             }
             catch(XmlSchemaException ex)
             {
                Console.WriteLine(ex.Message);
             }
             Console.Read();
          }
       }
    
    }
  2. Compile the code, and then run the code in Debug mode. You receive the following error message:
    The 'use' attribute must be optional (or absent) if the default attribute is present.
  3. Try one of the workarounds that are described in the inline comments. If you set no default value or if you set the column mapping as an element, you do not receive the exception error message.

Modification Type:MajorLast Reviewed:9/18/2003
Keywords:kbbug kbpending KB320881 kbAudDeveloper