-
March 2nd, 2011, 12:47 PM
#1
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:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Study_Aid_Schema"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Study">
<xs:complexType>
<xs:sequence>
<xs:element name="Subject" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="Topic" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="StudyAid" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<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>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
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.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<Study>
<Subject>
<Title>SampleSubject</Title>
<Topic>
<Title>SampleTopic</Title>
<StudyAid>
<Box>0</Box>
<LastLearned>2011\03\02</LastLearned>
<TimesLearned>0</TimesLearned>
<CorrectAnswer>A</CorrectAnswer>
<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>
</StudyAid>
</Topic>
</Subject>
</Study>
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.
Code:
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
DS_Aids.ReadXmlSchema("Study_Aid_Schema.xsd")
DS_Aids.ReadXml("Study_Aid.xml")
For Each Subject In DS_Aids.Tables("Subject")
lbSubjectBox.Items.Add(Subject.ToString)
Next Subject
End Sub
End Class
Anyone have any idea where I am going wrong?
-
March 4th, 2011, 01:13 AM
#2
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)
Code:
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)
streamReader.Close()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|