CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2002

    Schema information for element not found

    Im using VS2010 professional and am trying to teach myself how to use an XML file to store information.

    I have added an XSD file to the project with the following code:
    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="Study_Aid_Schema"
      <xs:element name="Study">
            <xs:element name="Subject" minOccurs="1" maxOccurs="unbounded">
                  <xs:element name="Title" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
                  <xs:element name="Topic" minOccurs="1" maxOccurs="unbounded">
                        <xs:element name="Title" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
                        <xs:element name="StudyAid" minOccurs="1" maxOccurs="unbounded">
                              <xs:element name="Box" type="xs:integer" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="LastLearned" type="xs:date" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="TimesLearned" type="xs:integer" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="CorrectAnswer" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="Field1" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="Field2" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="Field3" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="Field4" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
                              <xs:element name="Field5" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
    I've also added an XML file to the project and set the schema to Study_Aid_Schema.xsd in the properties pane. The XML file contains the following code.
    <?xml version="1.0" encoding="utf-8" ?>
            <Field1>This is the sample questions for sample 1</Field1>
            <Field2>This is the Sample A answer</Field2>
            <Field3>This is the Sample B answer</Field3>
            <Field4>This is the Sample C answer</Field4>
            <Field5>This is the Sample D answer</Field5>
    But when I try to access the information in the XML file with a data set I get the schema information for element not found error for every element in the XML file. This is the code I am using to try and access the data.
    Public Class Form2
        Private DS_Aids As New DataSet
        Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
            For Each Subject In DS_Aids.Tables("Subject")
            Next Subject
     End Sub
    End Class
    Anyone have any idea where I am going wrong?

  2. #2
    Join Date
    Feb 2005

    Re: Schema information for element not found

    I've not read into a dataset as you're trying to do, so I can't help you there (right now), but ....

    I would advice you to instead of reading in via datasets, to use the serialization and deserialzation techniques, which will populate your classes with the data from an XML file.
    You can even get the classes made for you because you have a schema file, using the xsd.exe component.

    To deserialize you need a class/classes made to contain the data in the structure of your schema. For example you can use xsd.exe (location differs depending on builds, mine's located in the path:
    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\xsd.exe
    or you can build it yourself if you're up to it
    As new to this, I'll recommend auto generating the classes and then understanding the structure - then you can always build/modify it yourself

    But once you have a class/classes you simply do (vb.net syntax as it looks like you're doing vb.net as well)
            Dim yourNewMainObject As <objectType>
            Using streamReader As New IO.StreamReader(<location of your XML file with data>)
                    Dim x As New XmlSerializer(GetType<objectType>))
                    yourNewMainObject = x.Deserialize(streamReader)
                End Using
    Now - the "yourNewMainObject" class should be filled with all the data in your XML file and can easily be used.
    The advantage of doing it this way is that you can also serialize your objects into XML in a similar method which will simplify persiting of data in XML.

    Hope this helps, otherwise I can try to make more of an example

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

By using this site, you agree to the Privacy Policy