Yanıt Xml Schema error

  • 16 Temmuz 2012 Pazartesi 14:43
     
      Kod İçerir

    I am using Visual Studio 2008 Pro and programming in Visual Basic and having some issues with Xml schemas.

    I’ve created an Xml schema file that I hope to use to validate an Xml data file. I want to be able to control whether and how the datafile is read when it contains an error. My goal is to validate and import the data (from the Xml file) to a dataSet and then process it. It looks like I am having problems with the the Xml shema. When I try to read the data I get errors for each element and line of data in the Xml table. The errors look like this:

     

    Validation Error: Could not find schema information for the element 'TFRTable'.

     

    I have included my code, Xml schema and a piece of the Xml data file below. Any plain language help would be much appreciated.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ds As New DataSet, rdr As XmlReader
    Dim myXmlFile As String = "C:\someFile.xml", schemaFile As String = " C:\someFile.xsd"
    rdr = readXmlWithXmlReader(schemaFile, myXmlFile)
    ds.ReadXml(rdr, XmlReadMode.IgnoreSchema)
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = ds.Tables(0).TableName
    End Sub
    Function readAndValidate(ByVal schemaFileStr As String, ByVal xmlFileStr As String) As XmlReader
            Dim xsc As XmlSchemaSet = New XmlSchemaSet()
            xsc.Add(Nothing, schemaFileStr)  
            Dim settings As New XmlReaderSettings()
            settings.ConformanceLevel = ConformanceLevel.Document
            settings.IgnoreWhitespace = True
            settings.IgnoreComments = True
            settings.ValidationType = ValidationType.Schema
            settings.Schemas = xsc
            settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
            AddHandler settings.ValidationEventHandler, AddressOf validator
            Dim reader As XmlReader = XmlReader.Create(xmlFileStr, settings)
           While reader.Read()
           End While
           Return reader
    End Function
    Sub validator(ByVal sender As Object, ByVal e As ValidationEventArgs)
            Console.WriteLine("Validation Error: {0}", e.Message)
    End Sub

    XML Schema:

    <?xml version="1.0" encoding="utf-8"?>

    <xs:schema id="FertSchema"

        targetNamespace="http://tempuri.org/FertSchema.xsd"

        elementFormDefault="qualified"

        xmlns="http://tempuri.org/FertSchema.xsd"

        xmlns:mstns="http://tempuri.org/FertSchema.xsd"

        xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:simpleType name="yearType" >

        <xs:restriction base="xs:unsignedShort">

          <xs:minInclusive value="1971" />

          <xs:maxInclusive value ="2101"/>

        </xs:restriction>

       </xs:simpleType>

      <xs:simpleType name="tfrType" >

        <xs:restriction base="xs:decimal">

          <xs:minInclusive value="0" />

          <xs:maxExclusive value="1" />

        </xs:restriction>

      </xs:simpleType>

      <xs:simpleType name="frType" >

        <xs:restriction base="xs:decimal">

          <xs:minInclusive value="0" />

          <xs:maxExclusive value="10" />

        </xs:restriction>

      </xs:simpleType>

        <xs:element name="TFRTable">

        <xs:complexType>

          <xs:sequence>

            <xs:element maxOccurs="unbounded" minOccurs="1" name="TFRRow">

              <xs:complexType>

                <xs:sequence>

                  <xs:element name="Year" minOccurs="1"  type="yearType"  />

                  <xs:element minOccurs="1"  name="tfr" type="tfrType"/>

                  <xs:element minOccurs="1"  name="fr1519" type="frType" />

                  <xs:element minOccurs="1"  name="fr2024" type="frType"/>

                  <xs:element minOccurs="1"  name="fr2529" type="frType"/>

                  <xs:element minOccurs="1"  name="fr3034" type="frType"/>

                  <xs:element minOccurs="1"  name="fr3539" type="frType"/>

                  <xs:element minOccurs="1"  name="fr4044" type="frType"/>

                  <xs:element minOccurs="1"  name="fr4549" type="frType"/>

                  <xs:element  minOccurs="1" name="mbr" type="frType" />

                </xs:sequence>

              </xs:complexType>

            </xs:element>

          </xs:sequence>

        </xs:complexType>

      </xs:element>

      </xs:schema>

    Extract from XML table file:

    <? xml version="1.0" encoding="UTF-8" standalone="yes"?>

    <TFRTable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <TFRRow>

    <Year>1971</Year>

    <tfr>2.1271</tfr>

    <fr1519>0.03988 </fr1519>

    <fr2024>0.1293</fr2024>

    <fr2529>0.13774</fr2529>

    <fr3034>0.07526</fr3034>

    <fr3539>0.03334</fr3539>

    <fr4044>0.0093</fr4044>

    <fr4549>0.0006</fr4549>

    <mbr>0.51314</mbr>

    </TFRRow

    .

    .

    </TFRTable>

    • Taşıyan Mark Liu-lxf 17 Temmuz 2012 Salı 07:38 (From:Visual Basic General)
    •  

Tüm Yanıtlar

  • 17 Temmuz 2012 Salı 07:38
     
     

    Hi Siansun,

    Thanks for you post.

    This forum is a VB.Net forum. There is a special forum to deal with XML issue. For better support, I will move this thread to XML, System.Xml, MSXML and XmlLite forum. It will cost a little time to involve the members in this forum. I appreciate your patience.

    Sorry for any incontinences and have a nice day.



    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • 17 Temmuz 2012 Salı 14:42
     
     
    OK. Noted. Thanks
  • 18 Temmuz 2012 Çarşamba 16:20
     
     

    Your schema has a target namespace in which it defines its elements, your XML instance document however does not use any namespace.

    And your .NET code enables warnings for the XmlReader, yet then you simply output Console.WriteLine("Validation Error: {0}", e.Message) without checking the e.Severity type.

    I think the validation event handler outputs a warning and that warning is correct, as your instance document has a root element named "TFRTable" in no namespace while your schema wants an element of that name in the target namespace. So you could use

      <TFRTable xmlns="http://tempuri.org/FertSchema.xsd">...</TFRTable>

    in your instance document, that should avoid the warning the validating XmlReader emits to your validation event handler.


    MVP Data Platform Development My blog

  • 18 Temmuz 2012 Çarşamba 17:28
     
      Kod İçerir

    Martin, thanks for that. You're right. It was a warning and not an error. Your suggestion did remove the warning. By the way, in case you tried to reproduce what I did, there was an error in the xml code listing: in the orginal listing the maxExclusive value for "tfrType" was set to 1; it should have been set to 10, as shown below.

    I still have a problem with the code, however; the data does not get read into the dataSet, so I end up with an empty dataSet. Any thoughts/suggestions?

    Thanks

     <xs:simpleType name="tfrType" >
        <xs:restriction base="xs:decimal">
          <xs:minInclusive value="0" />
          <xs:maxExclusive value="10" />
        </xs:restriction>
      </xs:simpleType>

  • 18 Temmuz 2012 Çarşamba 17:49
     
     

    Please explain in more detail what you want to achieve, usually you would either use a validating XmlReader to validate an XML document or you would use a DataSet with ReadXmlSchema first, then ReadXml to load data in a DataSet. Admittedly the second way does not validate all the items that a validating XmlReader checks.

    As for your code, you seem to want to use an XmlReader for validation to be passed in to ReadXml, that is not going to work as XmlReader is a forwards only, one time way to read through an XML document. If you use one XmlReader to read through the XML document and then pass that XmlReader on to another method then there are no more nodes to be consumed by that other method. Thus if you really want to validate first and then read the document into a DataSet I would suggest that you set up your XmlReader without doing the while Read() loop, instead you pass in the XmlReader to the ReadXml method of the DataSet, that way the DataSet should consume the XmlReader and the reader will do the validation. Or as an alternative separate the two steps, use your XmlReader for validation, then simply do DataSet.ReadXml(fileLocation) to read the XML into the data set.

    MVP Data Platform Development My blog

  • 19 Temmuz 2012 Perşembe 14:56
     
     Yanıt Kod İçerir

    Martin your comments were very helpful. I removed the 'While Read()' loop and everything worked fine. I also found an alternate solution that involves reading the Xml file and Xsd file each into an XDocument, then using the 'Validate' extension method on the Xml-document.

    Sub validateFile(ByVal xmlDoc As XDocument, ByVal xsdDoc As XDocument)
    '== Validate the xml file
    Dim schemaSet As New XmlSchemaSet schemaSet.Add(Nothing, xsdDoc.CreateReader)
    xmlDoc.Validate(schemaSet, AddressOf validator)
    End Sub
    

     

    Sub validator(ByVal sender As Object, ByVal e As ValidationEventArgs) If (e.Severity = XmlSeverityType.Warning) Then Console.WriteLine("Warning: " & e.Message) Else Console.WriteLine("Validation error message: " & vbCrLf & e.Message) Exit Sub End If End Sub

    • Yanıt Olarak İşaretleyen siansun 19 Temmuz 2012 Perşembe 14:56
    •