Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'ref' attribute cannot be present. validation error in C#

I did some xml Xsd validation according to following method: Xml validation using XSD schema

 .......................................................
 XmlReaderSettings settings = new XmlReaderSettings();
 settings.Schemas.Add(null, xsdFilePath);
 settings.ValidationType = ValidationType.Schema;
 settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
 XmlDocument document = new XmlDocument();
 document.Load(xmlFilePath);
 XmlReader rdr = XmlReader.Create(new StringReader(document.InnerXml), settings);

 while (rdr.Read())
 {

 }
 ...........................................................

and it gives me error saying: "The 'ref' attribute cannot be present"

my XSD looks like :

...........
<xs:element name="totals" minOccurs="0" ref="DocTotal"/>
..................................

<xs:element name="DocTotal">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="totalQty" minOccurs="0" type="xs:decimal"/>
            <xs:element name="totalTax" minOccurs="0" type="xs:decimal"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>
</xs:element>

and my xml looks like:

<totals>
    <totalQty>800</totalQty>
    <totalTax>0.00<totalTax>
</totals>

I believe this error occurs because of the both "name" and "ref": attributes exists in same elements: however I think this is not wrong in XSD(appreciate your comments on this): in this case is there any way to validate this XSD with xml:

like image 417
Bathiya Ladduwahetty Avatar asked Jun 24 '26 08:06

Bathiya Ladduwahetty


1 Answers

It looks to me like DocTotal ought to be a type, not an element:

<xs:element name="totals" minOccurs="0" type="DocTotal"/>
..................................

<xs:complexType name="DocTotal">
    <xs:sequence>
      <xs:element name="totalQty" minOccurs="0" type="xs:decimal"/>
      <xs:element name="totalTax" minOccurs="0" type="xs:decimal"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>

If you want to define the structure of an element somewhere (but not its name), and reference it elsewhere, it should be a type.

like image 72
Damien_The_Unbeliever Avatar answered Jun 26 '26 20:06

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!