Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# XML Schema validation

I have a nice XML file like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <Assets Path="C:\Users\r3plica\Google Drive">
        <Assetd>
            <FileName>Boomerang - Error codes.xlsx</FileName>
            <DisplayName>Boomerang - Error codes</DisplayName>
            <Description>This is the Boomerang error codes file</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Boomerang</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Assetd>
        <Asset>
            <FileName>Issue Tracker v5.xlsx</FileName>
            <Description>This is the issue tracker for Skipstone</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Skipstone</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Asset>
    </Assets>

and then I have my schema which I created like this:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="data"
        targetNamespace="http://tempuri.org/data.xsd"
        elementFormDefault="qualified"
        xmlns="http://tempuri.org/data.xsd"
        xmlns:mstns="http://tempuri.org/data.xsd"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
    >
      <xs:element name="Assets">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Asset" type="Asset" minOccurs="1" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>

      <xs:complexType name="Asset">
        <xs:sequence>
          <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1" />
          <xs:element name="DisplayName" type="xs:string" minOccurs="0" maxOccurs="1" />
          <xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="1" />
          <xs:element name="Tags" type="Tags" minOccurs="0" maxOccurs="1" />
          <xs:element name="Categories" type="Categories" minOccurs="1" maxOccurs="1" />
        </xs:sequence>
      </xs:complexType>

      <xs:complexType name="Tags">
        <xs:sequence>
          <xs:element name="Tag" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
      </xs:complexType>

      <xs:complexType name="Categories">
        <xs:sequence>
          <xs:element name="Category" type="xs:int" minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
      </xs:complexType>
    </xs:schema>

As far as I can see looking at that, the xml file is invalid because the first element is Assetd and not Asset, but if I run my c# code:

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://tempuri.org/data.xsd", "data.xsd");

XDocument doc = XDocument.Load(openFileDialog1.FileName);
string msg = "";
doc.Validate(schemas, (o, err) =>
{
    msg = err.Message;
});
Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg);

it tells me the xml is valid... If I use this code:

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add("http://tempuri.org/data.xsd", "data.xsd");
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(openFileDialog1.FileName, settings);

// Parse the file. 
while (reader.Read()) ;

I get this output in the console:

Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Assets'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the attribute 'Path'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Assetd'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'FileName'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'DisplayName'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Description'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tags'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Categories'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Asset'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'FileName'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Description'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tags'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Categories'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.

Can anyone tell me what I am doing wrong please? It is killing me :(

cheers, /r3plica

like image 363
r3plica Avatar asked Apr 01 '13 10:04

r3plica


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You need to set default namespace in your xml, like this:

<?xml version="1.0" encoding="utf-8"  ?>
    <Assets xmlns="http://tempuri.org/data.xsd">
        <Asset>
            <FileName>Boomerang - Error codes.xlsx</FileName>
            <DisplayName>Boomerang - Error codes</DisplayName>
            <Description>This is the Boomerang error codes file</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Boomerang</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Asset>
        <Asset>
            <FileName>Issue Tracker v5.xlsx</FileName>
            <Description>This is the issue tracker for Skipstone</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Skipstone</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Asset>
    </Assets>

Also, there is a number of other problems:

Path attribute is not defined in schema, 'Assetd' element is not defined. maxOccurs="unbounded" need to be set in schema for xs:element name="Asset"

In case if you cannot modify xml, you need to remove target schema from xsd:

<xs:schema id="data"
    xmlns:mstns="http://tempuri.org/data.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

And register schema like this:

settings.Schemas.Add(null, "data.xsd");
like image 150
Alexander Avatar answered Sep 29 '22 17:09

Alexander