user3604347
user3604347

Reputation: 91

Error while validating XML

System.ArgumentNullException: value cannot be undefined

StackTrace:

   at System.Xml.Linq.XAttribute..ctor(XName name, Object value)
   at System.Xml.Schema.XNodeValidator.ValidateAttributes(XElement e)
   at System.Xml.Schema.XNodeValidator.ValidateElement(XElement e)
   at System.Xml.Schema.XNodeValidator.ValidateNodes(XElement e)
   at System.Xml.Schema.XNodeValidator.ValidateElement(XElement e)
   at System.Xml.Schema.XNodeValidator.Validate(XObject source, XmlSchemaObject partialValidationType, Boolean addSchemaInfo)
   at System.Xml.Schema.Extensions.Validate(XDocument source, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, Boolean addSchemaInfo)

Source code:

    var xmlPath = @"C:\XSDTEST\test.xml";

    XDocument doc = XDocument.Load(xmlPath);
    XmlSchemaSet xss = new XmlSchemaSet();

    xss.Add("",@"C:\XSDTEST\test.xsd");

    XmlReaderSettings xrs = new XmlReaderSettings();
    xrs.ValidationType = ValidationType.Schema;
    xrs.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
    xrs.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
    xrs.ValidationFlags |= XmlSchemaValidationFlags.ProcessIdentityConstraints;
    xrs.Schemas = xss;

    doc.Validate(xss, new ValidationEventHandler((s, args) => { }), true);

test.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Child1" minOccurs="1" maxOccurs="1"/>
                <xsd:element ref="Child2" minOccurs="1" maxOccurs="1"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Child2" type="Child2ElemType"/>
    <xsd:complexType name="Child2ElemType">
        <xsd:attribute ref="align" default="left"/>
    </xsd:complexType>
    <xsd:attribute name="align" type="alignAttType"/>
    <xsd:simpleType name="alignAttType">
        <xsd:restriction base="xsd:NMTOKEN">
            <xsd:enumeration value="left"/>
            <xsd:enumeration value="right"/>
            <xsd:enumeration value="center"/>
            <xsd:enumeration value="justify"/>
            <xsd:enumeration value="char"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

test.xml:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/XSDTEST/test.xsd">
    <Child1/>
    <Child2/>
</Root>

The problem: default="left":

<xsd:attribute ref="align" default="left"/>

I think the validation process tries to create an "align" attribute with a default value, but the XAttribute constructor gets null, not "left".

If I set default value at <xsd:attribute name="align" type="alignAttType" default="left"/> it works fine.

If I set default value at <xsd:attribute ref="align" default="left"/> I will get the error.

Can I disable the creation of attributes with default values during the validation process?

or

What are the settings for correctly handling the default values?

Upvotes: 1

Views: 413

Answers (2)

user3604347
user3604347

Reputation: 91

After loading schemas I founded temporary workaround:

       foreach (XmlSchema schema in xss.Schemas())
            {
                foreach (System.Collections.DictionaryEntry ag in schema.AttributeGroups)
                {
                    if (ag.Value is XmlSchemaAttributeGroup)
                    {
                        var attributeGroup = (XmlSchemaAttributeGroup)ag.Value;


                        foreach (var attributeOrGroup in attributeGroup.Attributes)
                        {
                            if (attributeOrGroup is XmlSchemaAttribute)
                            {
                                var attribute = (XmlSchemaAttribute)attributeOrGroup;
                                if (attribute.DefaultValue != null)
                                {
                                    attribute.DefaultValue = null;
                                }
                                if (attribute.FixedValue != null)
                                {
                                    attribute.FixedValue = null;
                                }
                            }
                        }
                    }
                }
                foreach (System.Collections.DictionaryEntry st in schema.SchemaTypes)
                {
                    if (st.Value is XmlSchemaComplexType)
                    {
                        var c = (XmlSchemaComplexType)st.Value;

                        foreach (var g in c.Attributes)
                        {
                            if (g is XmlSchemaAttribute)
                            {
                                var attr = (XmlSchemaAttribute)g;
                                if (attr.DefaultValue != null)
                                {
                                    attr.DefaultValue = null;

                                }
                                if (attr.FixedValue != null)
                                {
                                    attr.FixedValue = null;
                                }

                            }
                        }
                    }
                }
            }

Upvotes: 0

Subbu
Subbu

Reputation: 2205

Based on the XSD, the "ref=" must be a "Qualified Name"

https://msdn.microsoft.com/en-us/library/ms256143(v=vs.110).aspx

The ref value must be a qualified name (QName)

Since you have an XSD without a namespace, it looks like the validator is not able to find the referenced attribute.

Also, take a look at this related SO question: How to reference an attribute in xsd

in XML Schemas all global element, attribute or type definitions must be qualified

The following is the relevant Standard extract: https://www.w3.org/TR/REC-xml-names/#defaulting

Default namespace declarations do not apply directly to attribute names

The namespace name for an unprefixed attribute name always has no value

The following link talks specifically about the unqualified global attributes Unqualified XSD global attribute references

Upvotes: 1

Related Questions