locked
Validating xml against XSD RRS feed

  • Question

  • Hi there,

    I have the following question. I have a main schema test.xsd that refers to two other  schemas ( test1.xsd & test2.xsd). I want to validate my xml against these xsd's. How can we do that?  In test1.xsd and test2.xsd, i have all the elements and attributes have been defined and they are being referred using the ref attribute.

    I want to do the following: ( Read XML, validate it against a schema and load it into a dataset, which is not working due to multiple schemas)
    objXmlDataDocument.DataSet.ReadXmlSchema(
    "C:\test.xsd")
    objXmlDataDocument.Load(
    "C:\test.xml")
    dsXmlData = objXmlDataDocument.DataSet

    Any help would be appreciated.

    Thanks,
    vsr
    Wednesday, March 10, 2010 2:43 PM

Answers

  • XML Schema has a lot of features.  Most of them are not supported by the DataSet.  I think (but am not sure) that the only schemas that ReadXmlSchema will take are those specifically written by the DataSet WriteXmlSchema method.

    Is your requirement simply to validate documents or do really need the data in a DataSet?
    Friday, March 12, 2010 11:41 PM

All replies

  • I think this is the case, but I'm not 100% sure:  I think the DataSet ReadXmlSchema only supports schemas written through the DataSet WriteXmlSchema method.  Your question becomes a "non issue" because you can't load arbitrary schemas into the DataSet.

    The DataSet only supports a limited subset of XML processing functionality.  For more functionality, use the tree-structured APIs such as the newer System.Xml.Linq.XDocument or the older System.Xml.XmlDocument.  Validation typically involves adding all of the schema files into the Schemas collection of the XmlReaderSettings object that you use when you construct the XmlReader.  Feel free to write back with more questions if you need help / more details on getting validation to work with these APIs.

    Thursday, March 11, 2010 12:38 AM
  • Thank you very much for the reply. I tried to rewrite the code using the XMLTexReader and XMLReaderSettings

    Dim xtrInputXml As XmlTextReader
    Dim xrsInputXml As XmlReaderSettings

    xrsInputXml = New XmlReaderSettings()
    xrsInputXml.Schemas.Add("URI","C:\test.xsd")

    xrsInputXml.ValidationType = ValidationType.Schema

    xtrInputXml = XmlTextReader.Create("C:\Input.xml",xrsInputXml)

    During this process i found out that my xsd's are the ones giving errors. I have a test.xsd which is refering to multiple xsd's. I am getting errors like some types not declared and some elements not declared.

    The whole problem seems to be with the include and imports i am using in the XSD. Any suggestions on how we can make sure that all the imports and includes in the xsds work correctly.

    I appreciate your help in this regard.

    Thursday, March 11, 2010 3:11 PM
  • Hi,

    If at all possible please share some repro of your problems, then we should be able to help you. But given that so far you just describe the problem as "having trouble including schemas", it's hard to suggest a solution.

    Thanks,
    Vitek Karas [MSFT]
    Thursday, March 11, 2010 10:49 PM
    Moderator
  • Trying adding all .xsd files to the Schemas collection, not just the main schema.

    Write back with the full error message if you continue to have difficulties.


    Friday, March 12, 2010 12:07 AM
  • Hi there,

    Thank you very much for the reply. I am trying to create a single schema from all the multiple schemas as i was unable to work with many of them. I was able to create a xsd without any error. I got the following error, when i try to read the schema into the dataset. Do you have any idea on this? Why does it occur when we are reading the xml?

    a relation named already belongs to this dataset xsd


    Thanks
    Friday, March 12, 2010 7:06 PM
  • XML Schema has a lot of features.  Most of them are not supported by the DataSet.  I think (but am not sure) that the only schemas that ReadXmlSchema will take are those specifically written by the DataSet WriteXmlSchema method.

    Is your requirement simply to validate documents or do really need the data in a DataSet?
    Friday, March 12, 2010 11:41 PM