.NETGURU
Help needed tying XML document to Schema
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngxml' list.


Bob Herrmann
Hi all,

I create an XML file and schema from an Access database table. Here is how the XML file looks when I display it using Internet Explorer: My question is: how does this xml file tie to my schema file?

-<XMLTopFolder>
- <tabTopFolder>
<TopFolderID>1</TopFolderID>
<TopFolderName>[do not delete]</TopFolderName>
<SequenceNum>1</SequenceNum>
<Link>//http.www.bob</Link>
<ShowInContentsFlag>false</ShowInContentsFlag>
</tabTopFolder>
- <tabTopFolder>
<TopFolderID>2</TopFolderID>
<TopFolderName>Walking the Spiritual Path</TopFolderName>
<SequenceNum>1</SequenceNum>
<Link>//http.www.bob</Link>
<ShowInContentsFlag>true</ShowInContentsFlag>
</tabTopFolder>
</XMLTopFolder>

I I am confused on how this ties to my schema file. I am sure I created the xml file incorrectly. I am new to xml. Here is my code that creates the xml and schema files:

Private Sub WriteToXmlFile(ByVal thisDataSet As DataSet, ByVal xFileName As String, ByVal xSchemaName As String)
If thisDataSet Is Nothing Then
Return
End If
Dim myFileStream As New System.IO.FileStream _
(xFileName, System.IO.FileMode.Create)
Dim myFileStreamSchema As New System.IO.FileStream _
(xSchemaName, System.IO.FileMode.Create)
Dim myXmlWriter As New System.Xml.XmlTextWriter _
(myFileStream, System.Text.Encoding.Unicode)
Dim myXmlSchemaWriter As New System.Xml.XmlTextWriter _
(myFileStreamSchema, System.Text.Encoding.Unicode)
thisDataSet.WriteXml(myXmlWriter)
myXmlWriter.Close()
thisDataSet.WriteXmlSchema(myXmlSchemaWriter)
myXmlSchemaWriter.Close()
End Sub
Reply to this message...
 
    
Dan Wahlin
The schema document simply describes several things about the XML
including its structure, data types, etc. Schemas are useful for
validating what an XML document contains. So, if Company A sends me an
XML feed I could validate the document against a schema to ensure that
the data they sent is what my application was expecting to work with.

You don’t actually have to use the schema but it can be useful for
preloading meta-data into a DataSet so that all of the tables,
DataColumns, etc. are known before XML is loaded into the DataSet. It
can also be used along with the XmlValidatingReader in the System.Xml
namespace to validate an XML document programmatically. Plus schemas
can be used to create typed DataSets and classes which can simplify your
development. The following example shows how a schema can be used to
pre-fill a DataSet with metadata so that XML can be moved into a
Database:

HYPERLINK
"http://www.xmlforasp.net/codeSection.aspx?csID=37"" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=37";http://www.xmlforasp.
net/codeSection.aspx?csID=37

HTH,

Dan Wahlin

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

HYPERLINK "http://www.xmlforasp.net/"" target="_blank">http://www.xmlforasp.net/";http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 7:24 AM
To: aspngxml
Subject: [aspngxml] Help needed tying XML document to Schema

Hi all,

I create an XML file and schema from an Access database table. Here is
how the XML file looks when I display it using Internet Explorer: My
question is: how does this xml file tie to my schema file?

-<XMLTopFolder>

HYPERLINK "file:///C:\\XMLFiles\\TopFolderXmlDoc.xml"- <tabTopFolder>

<TopFolderID>1</TopFolderID>

<TopFolderName>[do not delete]</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>false</ShowInContentsFlag>

</tabTopFolder>

HYPERLINK "file:///C:\\XMLFiles\\TopFolderXmlDoc.xml"- <tabTopFolder>

<TopFolderID>2</TopFolderID>

<TopFolderName>Walking the Spiritual Path</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>true</ShowInContentsFlag>

</tabTopFolder>

</XMLTopFolder>

I I am confused on how this ties to my schema file. I am sure I created
the xml file incorrectly. I am new to xml. Here is my code that
creates the xml and schema files:

Private Sub WriteToXmlFile(ByVal thisDataSet As DataSet, ByVal xFileName
As String, ByVal xSchemaName As String)

If thisDataSet Is Nothing Then

Return

End If

Dim myFileStream As New System.IO.FileStream _

(xFileName, System.IO.FileMode.Create)

Dim myFileStreamSchema As New System.IO.FileStream _

(xSchemaName, System.IO.FileMode.Create)

Dim myXmlWriter As New System.Xml.XmlTextWriter _

(myFileStream, System.Text.Encoding.Unicode)

Dim myXmlSchemaWriter As New System.Xml.XmlTextWriter _

(myFileStreamSchema, System.Text.Encoding.Unicode)

thisDataSet.WriteXml(myXmlWriter)

myXmlWriter.Close()

thisDataSet.WriteXmlSchema(myXmlSchemaWriter)

myXmlSchemaWriter.Close()

End Sub

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002
Reply to this message...
 
    
Tim Curtin
at the root node:
<XMLTopFolder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="http://mycomp.com/myschemas/myschema.xsd";>

[Original message clipped]

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx

Reply to this message...
 
    
Bob Herrmann
Thanks for the explanation Dan. It does help me understand the usefullness of schemas. I have a problem when I try to load my xml file into my SQL database I get an error. The error happens when one of the elements is missing. But this should be okay because the data is optional. I have this element defined in my schema but I am not sure how to tell it that this element is optional. Then how do I actually run the validation to check for this? I would like to see some code snippets of how this is done if possible.

Thanks,
Bob

----- Original Message -----
From: Dan Wahlin
To: aspngxml
Sent: Tuesday, July 23, 2002 2:44 PM
Subject: [aspngxml] RE: Help needed tying XML document to Schema

The schema document simply describes several things about the XML including its structure, data types, etc. Schemas are useful for validating what an XML document contains. So, if Company A sends me an XML feed I could validate the document against a schema to ensure that the data they sent is what my application was expecting to work with.

You don’t actually have to use the schema but it can be useful for preloading meta-data into a DataSet so that all of the tables, DataColumns, etc. are known before XML is loaded into the DataSet. It can also be used along with the XmlValidatingReader in the System.Xml namespace to validate an XML document programmatically. Plus schemas can be used to create typed DataSets and classes which can simplify your development. The following example shows how a schema can be used to pre-fill a DataSet with metadata so that XML can be moved into a Database:

http://www.xmlforasp.net/codeSection.aspx?csID=37

HTH,

Dan Wahlin

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 7:24 AM
To: aspngxml
Subject: [aspngxml] Help needed tying XML document to Schema

Hi all,

I create an XML file and schema from an Access database table. Here is how the XML file looks when I display it using Internet Explorer: My question is: how does this xml file tie to my schema file?

-<XMLTopFolder>

- <tabTopFolder>

<TopFolderID>1</TopFolderID>

<TopFolderName>[do not delete]</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>false</ShowInContentsFlag>

</tabTopFolder>

- <tabTopFolder>

<TopFolderID>2</TopFolderID>

<TopFolderName>Walking the Spiritual Path</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>true</ShowInContentsFlag>

</tabTopFolder>

</XMLTopFolder>

I I am confused on how this ties to my schema file. I am sure I created the xml file incorrectly. I am new to xml. Here is my code that creates the xml and schema files:

Private Sub WriteToXmlFile(ByVal thisDataSet As DataSet, ByVal xFileName As String, ByVal xSchemaName As String)

If thisDataSet Is Nothing Then

Return

End If

Dim myFileStream As New System.IO.FileStream _

(xFileName, System.IO.FileMode.Create)

Dim myFileStreamSchema As New System.IO.FileStream _

(xSchemaName, System.IO.FileMode.Create)

Dim myXmlWriter As New System.Xml.XmlTextWriter _

(myFileStream, System.Text.Encoding.Unicode)

Dim myXmlSchemaWriter As New System.Xml.XmlTextWriter _

(myFileStreamSchema, System.Text.Encoding.Unicode)

thisDataSet.WriteXml(myXmlWriter)

myXmlWriter.Close()

thisDataSet.WriteXmlSchema(myXmlSchemaWriter)

myXmlSchemaWriter.Close()

End Sub

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002
Reply to this message...
 
    
Dan Wahlin
Hi Bob,

I have a web-based schema validation page at the following link that you
can use to validate the XML against the schema manually: HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/content.aspx?content=SchemaValidator"" target="_blank">http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/content.aspx?content=SchemaValidator";http://ww
w.xmlforasp.net/content.aspx?content=SchemaValidator. Or I’d recommend
downloading the demo of XML Spy which has built-in schema and validation
support:

HYPERLINK "http://www.xmlspy.com" target="_blank">http://www.xmlspy.com/"http://www.xmlspy.com" target="_blank">http://www.xmlspy.com

HYPERLINK
"http://www.fawcette.com/xmlmag/2002" target="_blank">http://www.fawcette.com/xmlmag/2002_07/online/xml_dwahlin_07_22_02/"htt
p://www.fawcette.com/xmlmag/2002_07/online/xml_dwahlin_07_22_02/

To programmatically validate the XML you’ll find an example (a reuseable
object) here that should give you a jump-start:

HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=27"" target="_blank">http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=27";http://www.xmlforasp" target="_blank">http://www.xmlforasp.
net/codeSection.aspx?csID=27

Also, when you load the schema into the DataSet using ReadXmlSchema()
and then load the XML using ReadXml() it should let you know of any
problems automatically.

HTH,

Dan

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

HYPERLINK "http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/"http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 12:08 PM
To: aspngxml
Subject: [aspngxml] RE: Help needed tying XML document to Schema

Thanks for the explanation Dan. It does help me understand the
usefullness of schemas. I have a problem when I try to load my xml file
into my SQL database I get an error. The error happens when one of the
elements is missing. But this should be okay because the data is
optional. I have this element defined in my schema but I am not sure
how to tell it that this element is optional. Then how do I actually
run the validation to check for this? I would like to see some code
snippets of how this is done if possible.

Thanks,

Bob

----- Original Message -----

From: HYPERLINK "mailto:Click here to reveal e-mail address"Dan Wahlin

To: HYPERLINK "mailto:Click here to reveal e-mail address"aspngxml

Sent: Tuesday, July 23, 2002 2:44 PM

Subject: [aspngxml] RE: Help needed tying XML document to Schema

The schema document simply describes several things about the XML
including its structure, data types, etc. Schemas are useful for
validating what an XML document contains. So, if Company A sends me an
XML feed I could validate the document against a schema to ensure that
the data they sent is what my application was expecting to work with.

You don’t actually have to use the schema but it can be useful for
preloading meta-data into a DataSet so that all of the tables,
DataColumns, etc. are known before XML is loaded into the DataSet. It
can also be used along with the XmlValidatingReader in the System.Xml
namespace to validate an XML document programmatically. Plus schemas
can be used to create typed DataSets and classes which can simplify your
development. The following example shows how a schema can be used to
pre-fill a DataSet with metadata so that XML can be moved into a
Database:

HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=37"http://www.xmlforasp" target="_blank">http://www.xmlforasp.
net/codeSection.aspx?csID=37

HTH,

Dan Wahlin

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

HYPERLINK "http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/"http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 7:24 AM
To: aspngxml
Subject: [aspngxml] Help needed tying XML document to Schema

Hi all,

I create an XML file and schema from an Access database table. Here is
how the XML file looks when I display it using Internet Explorer: My
question is: how does this xml file tie to my schema file?

-<XMLTopFolder>

HYPERLINK "file:///C:\\XMLFiles\\TopFolderXmlDoc.xml"- <tabTopFolder>

<TopFolderID>1</TopFolderID>

<TopFolderName>[do not delete]</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>false</ShowInContentsFlag>

</tabTopFolder>

HYPERLINK "file:///C:\\XMLFiles\\TopFolderXmlDoc.xml"- <tabTopFolder>

<TopFolderID>2</TopFolderID>

<TopFolderName>Walking the Spiritual Path</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>true</ShowInContentsFlag>

</tabTopFolder>

</XMLTopFolder>

I I am confused on how this ties to my schema file. I am sure I created
the xml file incorrectly. I am new to xml. Here is my code that
creates the xml and schema files:

Private Sub WriteToXmlFile(ByVal thisDataSet As DataSet, ByVal xFileName
As String, ByVal xSchemaName As String)

If thisDataSet Is Nothing Then

Return

End If

Dim myFileStream As New System.IO.FileStream _

(xFileName, System.IO.FileMode.Create)

Dim myFileStreamSchema As New System.IO.FileStream _

(xSchemaName, System.IO.FileMode.Create)

Dim myXmlWriter As New System.Xml.XmlTextWriter _

(myFileStream, System.Text.Encoding.Unicode)

Dim myXmlSchemaWriter As New System.Xml.XmlTextWriter _

(myFileStreamSchema, System.Text.Encoding.Unicode)

thisDataSet.WriteXml(myXmlWriter)

myXmlWriter.Close()

thisDataSet.WriteXmlSchema(myXmlSchemaWriter)

myXmlSchemaWriter.Close()

End Sub

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002
Reply to this message...
 
    
Bob Herrmann
Thanks for your help Tim. So you are saying that I need to add these lines
to my xml document? How come these lines are not added when I create my
xml document? I am probably missing a step.

Thanks,
Bob
----- Original Message -----
From: "Tim Curtin" <Click here to reveal e-mail address>
To: "aspngxml" <Click here to reveal e-mail address>
Sent: Tuesday, July 23, 2002 11:11 AM
Subject: [aspngxml] Re: Help needed tying XML document to Schema

[Original message clipped]

Reply to this message...
 
    
Bob Herrmann
Thanks Dan, I will check out these sites. I finally did get a validating routine written and I kind of see how this works. I wrote a new page to test this. I changed some elements in my schema to be minoccurs="1" and then deleted the element in my xml table and I captured the error. I then changed the schema to minoccurs="0" and no error occured. My problem is still how do I tie my xml file to the schema in my routine that dumps the xml file into my SQL database. Here is the code:

Dim oDoc As New XmlDataDocument()
Dim item As XmlElement
Dim items As XmlNodeList
Dim conString As String = "server=DEERFIELD\BOBSQL;uid=sa;pwd=bobsql;database=BobMS"
Dim con As New SqlConnection(conString)
con.Open()
oDoc.Load("c:\XMLFiles\TopFolderXmlDoc.xml")
Dim cmd As New SqlCommand()
items = oDoc.SelectNodes("//XMLTopFolder/tabTopFolder")
With cmd
.Connection = con
.CommandText = "sp_UpdateXMLTop"
.CommandType = CommandType.StoredProcedure
.Parameters.Add(New SqlParameter("@TopFolderID", SqlDbType.Int))
.Parameters.Add(New SqlParameter("@TopFolderName", SqlDbType.VarChar, 900))
.Parameters.Add(New SqlParameter("@SequenceNum", SqlDbType.Int))
.Parameters.Add(New SqlParameter("@Link", SqlDbType.VarChar, 100))
.Parameters.Add(New SqlParameter("@ShowInContentsFlag", SqlDbType.Bit))
For Each item In items
.Parameters("@TopFolderID").Value = item.SelectSingleNode("TopFolderID").InnerText
.Parameters("@TopFolderName").Value = item.SelectSingleNode("TopFolderName").InnerText
.Parameters("@SequenceNum").Value = item.SelectSingleNode("SequenceNum").InnerText
.Parameters("@Link").Value = item.SelectSingleNode("Link").InnerText
.Parameters("@ShowInContentsFlag").Value = item.SelectSingleNode("ShowInContentsFlag").InnerText
.ExecuteNonQuery()
Next
cmd = Nothing
End With

How do I allow things like the minoccurs="0" to happen in this chunk of code? Or do I need to rewrite this code with a different technique? Presently, I get an error on each element that is missing.

Thanks,
Bob
----- Original Message -----
From: Dan Wahlin
To: aspngxml
Sent: Wednesday, July 24, 2002 2:56 PM
Subject: [aspngxml] RE: Help needed tying XML document to Schema

Hi Bob,

I have a web-based schema validation page at the following link that you can use to validate the XML against the schema manually: http://www.xmlforasp.net/content.aspx?content=SchemaValidator. Or I’d recommend downloading the demo of XML Spy which has built-in schema and validation support:

http://www.xmlspy.com

http://www.fawcette.com/xmlmag/2002_07/online/xml_dwahlin_07_22_02/

To programmatically validate the XML you’ll find an example (a reuseable object) here that should give you a jump-start:

http://www.xmlforasp.net/codeSection.aspx?csID=27

Also, when you load the schema into the DataSet using ReadXmlSchema() and then load the XML using ReadXml() it should let you know of any problems automatically.

HTH,

Dan

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 12:08 PM
To: aspngxml
Subject: [aspngxml] RE: Help needed tying XML document to Schema

Thanks for the explanation Dan. It does help me understand the usefullness of schemas. I have a problem when I try to load my xml file into my SQL database I get an error. The error happens when one of the elements is missing. But this should be okay because the data is optional. I have this element defined in my schema but I am not sure how to tell it that this element is optional. Then how do I actually run the validation to check for this? I would like to see some code snippets of how this is done if possible.

Thanks,

Bob

----- Original Message -----

From: Dan Wahlin

To: aspngxml

Sent: Tuesday, July 23, 2002 2:44 PM

Subject: [aspngxml] RE: Help needed tying XML document to Schema

The schema document simply describes several things about the XML including its structure, data types, etc. Schemas are useful for validating what an XML document contains. So, if Company A sends me an XML feed I could validate the document against a schema to ensure that the data they sent is what my application was expecting to work with.

You don’t actually have to use the schema but it can be useful for preloading meta-data into a DataSet so that all of the tables, DataColumns, etc. are known before XML is loaded into the DataSet. It can also be used along with the XmlValidatingReader in the System.Xml namespace to validate an XML document programmatically. Plus schemas can be used to create typed DataSets and classes which can simplify your development. The following example shows how a schema can be used to pre-fill a DataSet with metadata so that XML can be moved into a Database:

http://www.xmlforasp.net/codeSection.aspx?csID=37

HTH,

Dan Wahlin

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 7:24 AM
To: aspngxml
Subject: [aspngxml] Help needed tying XML document to Schema

Hi all,

I create an XML file and schema from an Access database table. Here is how the XML file looks when I display it using Internet Explorer: My question is: how does this xml file tie to my schema file?

-<XMLTopFolder>

- <tabTopFolder>

<TopFolderID>1</TopFolderID>

<TopFolderName>[do not delete]</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>false</ShowInContentsFlag>

</tabTopFolder>

- <tabTopFolder>

<TopFolderID>2</TopFolderID>

<TopFolderName>Walking the Spiritual Path</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>true</ShowInContentsFlag>

</tabTopFolder>

</XMLTopFolder>

I I am confused on how this ties to my schema file. I am sure I created the xml file incorrectly. I am new to xml. Here is my code that creates the xml and schema files:

Private Sub WriteToXmlFile(ByVal thisDataSet As DataSet, ByVal xFileName As String, ByVal xSchemaName As String)

If thisDataSet Is Nothing Then

Return

End If

Dim myFileStream As New System.IO.FileStream _

(xFileName, System.IO.FileMode.Create)

Dim myFileStreamSchema As New System.IO.FileStream _

(xSchemaName, System.IO.FileMode.Create)

Dim myXmlWriter As New System.Xml.XmlTextWriter _

(myFileStream, System.Text.Encoding.Unicode)

Dim myXmlSchemaWriter As New System.Xml.XmlTextWriter _

(myFileStreamSchema, System.Text.Encoding.Unicode)

thisDataSet.WriteXml(myXmlWriter)

myXmlWriter.Close()

thisDataSet.WriteXmlSchema(myXmlSchemaWriter)

myXmlSchemaWriter.Close()

End Sub

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002
Reply to this message...
 
    
Dan Wahlin
Before you run your update routine you would want to validate the XML
against the schema using the XmlValidatingReader and ensure everything
is correct. If it’s not you could then log errors to a file so you know
what’s wrong.

To validate you can either by add the schema reference into the XML doc
as Tim pointed out or use the XmlSchemaCollection object as shown in the
example I sent earlier to dynamically assign the schema.

Although you can use the XmlDocument to access the data in the XML and
then perform the updates, you’ll want to be careful that your XML
document isn’t very large and that you don’t process many at once.
Since the XmlDocument works by loading into memory you may run into a
few issues. If you want to be more scalable you can use the
XmlTextReader to parse the XML and access the data or just load the
schema and XML into the DataSet and use a DataAdapter to update or
insert the data. Here’s an example of the using the XmlTextReader to
parse and insert data into a db:

HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=11"http://www.xmlforasp" target="_blank">http://www.xmlforasp.
net/codeSection.aspx?csID=11

Another example of using the DataSet to load the schema and XML and then
update the db can be found here (this is a little complex since XSLT is
also involved but it should help you see how the DataSet can simplify
things for you:

HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=37"http://www.xmlforasp" target="_blank">http://www.xmlforasp.
net/codeSection.aspx?csID=37

Here’s a simpler example of using the DataSet to load XML schema and
document and then update the db (sorry…it’s in C# but you can use Alex’s
C# to VB.NET converter:
http://www.aspalliance.com/aldotnet/examples/translate.aspx" target="_blank">http://www.aspalliance.com/aldotnet/examples/translate.aspx):

private void Page_Load(object sender, System.EventArgs e) {

bool status = ProcessXml();

string statusString = (status==true)?"Succeeded":"Failed";

this.lblStatus.Text = "<b>XML Insertion " + statusString +
"!</b>";

}

private bool ProcessXml() {

string xmlPath = Server.MapPath("XML/CustomersData.xml");

string xmlSchemaPath Server.MapPath("Schemas/CustomersSchema.xsd");

//Fill ds with schema

DataSet ds = new DataSet("CustomersData");

ds.ReadXmlSchema(xmlSchemaPath);

//load XML

ds.ReadXml(xmlPath);

//perform db update

bool status = InsertXMLIntoDatabase(ds);

return status;

}

private bool InsertXMLIntoDatabase(DataSet ds) {

string connStr = "server=localhost;uid=sa;pwd=;initial
catalog=Northwind";

string sql = "SELECT * FROM Customers";

//Track if insert succeeds or fails

bool insertStatus = true;

//Connect to db

SqlConnection dataConn = new SqlConnection(connStr);

//Create SqlDataAdapter used for insert of data within
DataSet

SqlDataAdapter da = new SqlDataAdapter(sql,dataConn);

//Save ourself the trouble of wiring up the InsertCommand

//on the SqlDataAdapter by using a SqlCommandBuilder instead

SqlCommandBuilder cb = new SqlCommandBuilder(da);

try {

//Call the DataAdapter's Update() method to
perform the inserts

da.Update(ds,"Customers");

}

catch (Exception exp) {

//Catch any errors such as duplicate primary key

//insertion attempts

Response.Write(exp.Message);

insertStatus = false;

}

finally {

if (dataConn.State != ConnectionState.Closed)
dataConn.Close();

}

return insertStatus;

}

HTH,

Dan

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

HYPERLINK "http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/"http://www.XMLforASP.NET" target="_blank">http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Wednesday, July 24, 2002 12:29 PM
To: aspngxml
Subject: [aspngxml] RE: Help needed tying XML document to Schema

Thanks Dan, I will check out these sites. I finally did get a
validating routine written and I kind of see how this works. I wrote a
new page to test this. I changed some elements in my schema to be
minoccurs="1" and then deleted the element in my xml table and I
captured the error. I then changed the schema to minoccurs="0" and no
error occured. My problem is still how do I tie my xml file to the
schema in my routine that dumps the xml file into my SQL database. Here
is the code:

Dim oDoc As New XmlDataDocument()

Dim item As XmlElement

Dim items As XmlNodeList

Dim conString As String "server=DEERFIELD\BOBSQL;uid=sa;pwd=bobsql;database=BobMS"

Dim con As New SqlConnection(conString)

con.Open()

oDoc.Load("c:\XMLFiles\TopFolderXmlDoc.xml")

Dim cmd As New SqlCommand()

items = oDoc.SelectNodes("//XMLTopFolder/tabTopFolder")

With cmd

.Connection = con

.CommandText = "sp_UpdateXMLTop"

.CommandType = CommandType.StoredProcedure

.Parameters.Add(New SqlParameter("@TopFolderID", SqlDbType.Int))

.Parameters.Add(New SqlParameter("@TopFolderName", SqlDbType.VarChar,
900))

.Parameters.Add(New SqlParameter("@SequenceNum", SqlDbType.Int))

.Parameters.Add(New SqlParameter("@Link", SqlDbType.VarChar, 100))

.Parameters.Add(New SqlParameter("@ShowInContentsFlag",
SqlDbType.Bit))

For Each item In items

.Parameters("@TopFolderID").Value item.SelectSingleNode("TopFolderID").InnerText

.Parameters("@TopFolderName").Value item.SelectSingleNode("TopFolderName").InnerText

.Parameters("@SequenceNum").Value item.SelectSingleNode("SequenceNum").InnerText

.Parameters("@Link").Value item.SelectSingleNode("Link").InnerText

.Parameters("@ShowInContentsFlag").Value item.SelectSingleNode("ShowInContentsFlag").InnerText

.ExecuteNonQuery()

Next

cmd = Nothing

End With

How do I allow things like the minoccurs="0" to happen in this chunk of
code? Or do I need to rewrite this code with a different technique?
Presently, I get an error on each element that is missing.

Thanks,

Bob

----- Original Message -----

From: HYPERLINK "mailto:Click here to reveal e-mail address"Dan Wahlin

To: HYPERLINK "mailto:Click here to reveal e-mail address"aspngxml

Sent: Wednesday, July 24, 2002 2:56 PM

Subject: [aspngxml] RE: Help needed tying XML document to Schema

Hi Bob,

I have a web-based schema validation page at the following link that you
can use to validate the XML against the schema manually: HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/content.aspx?content=SchemaValidator"" target="_blank">http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/content.aspx?content=SchemaValidator";http://ww
w.xmlforasp.net/content.aspx?content=SchemaValidator. Or I’d recommend
downloading the demo of XML Spy which has built-in schema and validation
support:

HYPERLINK "http://www.xmlspy.com" target="_blank">http://www.xmlspy.com"" target="_blank">http://www.xmlspy.com" target="_blank">http://www.xmlspy.com";http://www.xmlspy.com" target="_blank">http://www.xmlspy.com

HYPERLINK
"http://www.fawcette.com/xmlmag/2002" target="_blank">http://www.fawcette.com/xmlmag/2002_07/online/xml_dwahlin_07_22_02/"htt
p://www.fawcette.com/xmlmag/2002_07/online/xml_dwahlin_07_22_02/

To programmatically validate the XML you’ll find an example (a reuseable
object) here that should give you a jump-start:

HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=27"http://www.xmlforasp" target="_blank">http://www.xmlforasp.
net/codeSection.aspx?csID=27

Also, when you load the schema into the DataSet using ReadXmlSchema()
and then load the XML using ReadXml() it should let you know of any
problems automatically.

HTH,

Dan

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

HYPERLINK "http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/"http://www.XMLforASP.NET" target="_blank">http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 12:08 PM
To: aspngxml
Subject: [aspngxml] RE: Help needed tying XML document to Schema

Thanks for the explanation Dan. It does help me understand the
usefullness of schemas. I have a problem when I try to load my xml file
into my SQL database I get an error. The error happens when one of the
elements is missing. But this should be okay because the data is
optional. I have this element defined in my schema but I am not sure
how to tell it that this element is optional. Then how do I actually
run the validation to check for this? I would like to see some code
snippets of how this is done if possible.

Thanks,

Bob

----- Original Message -----

From: HYPERLINK "mailto:Click here to reveal e-mail address"Dan Wahlin

To: HYPERLINK "mailto:Click here to reveal e-mail address"aspngxml

Sent: Tuesday, July 23, 2002 2:44 PM

Subject: [aspngxml] RE: Help needed tying XML document to Schema

The schema document simply describes several things about the XML
including its structure, data types, etc. Schemas are useful for
validating what an XML document contains. So, if Company A sends me an
XML feed I could validate the document against a schema to ensure that
the data they sent is what my application was expecting to work with.

You don’t actually have to use the schema but it can be useful for
preloading meta-data into a DataSet so that all of the tables,
DataColumns, etc. are known before XML is loaded into the DataSet. It
can also be used along with the XmlValidatingReader in the System.Xml
namespace to validate an XML document programmatically. Plus schemas
can be used to create typed DataSets and classes which can simplify your
development. The following example shows how a schema can be used to
pre-fill a DataSet with metadata so that XML can be moved into a
Database:

HYPERLINK
"http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/codeSection.aspx?csID=37"http://www.xmlforasp" target="_blank">http://www.xmlforasp.
net/codeSection.aspx?csID=37

HTH,

Dan Wahlin

Wahlin Consulting LLC

Microsoft MVP - ASP.NET

HYPERLINK "http://www.xmlforasp" target="_blank">http://www.xmlforasp.net/"http://www.XMLforASP.NET" target="_blank">http://www.XMLforASP.NET

XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Bob Herrmann [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 23, 2002 7:24 AM
To: aspngxml
Subject: [aspngxml] Help needed tying XML document to Schema

Hi all,

I create an XML file and schema from an Access database table. Here is
how the XML file looks when I display it using Internet Explorer: My
question is: how does this xml file tie to my schema file?

-<XMLTopFolder>

HYPERLINK "file:///C:\\XMLFiles\\TopFolderXmlDoc.xml"- <tabTopFolder>

<TopFolderID>1</TopFolderID>

<TopFolderName>[do not delete]</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>false</ShowInContentsFlag>

</tabTopFolder>

HYPERLINK "file:///C:\\XMLFiles\\TopFolderXmlDoc.xml"- <tabTopFolder>

<TopFolderID>2</TopFolderID>

<TopFolderName>Walking the Spiritual Path</TopFolderName>

<SequenceNum>1</SequenceNum>

<Link>//http.www.bob</Link>

<ShowInContentsFlag>true</ShowInContentsFlag>

</tabTopFolder>

</XMLTopFolder>

I I am confused on how this ties to my schema file. I am sure I created
the xml file incorrectly. I am new to xml. Here is my code that
creates the xml and schema files:

Private Sub WriteToXmlFile(ByVal thisDataSet As DataSet, ByVal xFileName
As String, ByVal xSchemaName As String)

If thisDataSet Is Nothing Then

Return

End If

Dim myFileStream As New System.IO.FileStream _

(xFileName, System.IO.FileMode.Create)

Dim myFileStreamSchema As New System.IO.FileStream _

(xSchemaName, System.IO.FileMode.Create)

Dim myXmlWriter As New System.Xml.XmlTextWriter _

(myFileStream, System.Text.Encoding.Unicode)

Dim myXmlSchemaWriter As New System.Xml.XmlTextWriter _

(myFileStreamSchema, System.Text.Encoding.Unicode)

thisDataSet.WriteXml(myXmlWriter)

myXmlWriter.Close()

thisDataSet.WriteXmlSchema(myXmlSchemaWriter)

myXmlSchemaWriter.Close()

End Sub

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp" target="_blank">http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com" target="_blank">http://www.grisoft.com).
Version: 6.0.377 / Virus Database: 211 - Release Date: 07/15/2002
Reply to this message...
 
    
Tim Curtin
I would change the schema to set a default if no element exists. That way
the schema will return a node to the parser if the file does not exist. OR,
have your trading partner supply an empty node if no data exists. Then, set
the minoccurs="1". The code will blow up if no element exists in the XML.
XMLSPY is a super tool to assist with the syntax.

[Original message clipped]

_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com

Reply to this message...
 
    
Bob Herrmann
Dan, I got it!! Finally! thanks for your help. Like I said, this was my first attempt at XML and I probably bit off more than I could chew for my first project but now everything works. I am going to review all my code now and try to streamline it as much as I can. I am going to buy your book also so that I can become more efficient at XML. It looks like my company is going to get into this heavily.

Thanks again,
Bob
Reply to this message...
 
    
Tim Curtin
Hi Bob,
did you rewrite your code to use the XMLTextReader or the code I gave you?
What was your solution?

Tim

[Original message clipped]

_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail.
http://www.hotmail.com

Reply to this message...
 
 
System.Data.CommandType
System.Data.Common.DataAdapter
System.Data.ConnectionState
System.Data.DataSet
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlCommandBuilder
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlDataAdapter
System.Data.SqlClient.SqlParameter
System.Data.SqlDbType
System.EventArgs
System.IO.FileMode
System.IO.FileStream
System.Text.Encoding
System.Xml.Schema.XmlSchemaCollection
System.Xml.XmlDataDocument
System.Xml.XmlDocument
System.Xml.XmlElement
System.Xml.XmlNodeList
System.Xml.XmlTextReader
System.Xml.XmlTextWriter
System.Xml.XmlValidatingReader




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification