Hi. Until my latest project I have been a php MySQl man - and not touched xml. I have had a fair indoctrination with
Imar Spaanjars Beginning ASP.Net 3.5 in C# and VB. I chose to go the
VB route. I freely confess I still have a lot to learn.
I have a specific need to handle xml and so got stuck into this book's chapter 10.
In Visual Web Developer 2008 Express edition, I ran listing 10-10_app and all was well. I then tried to expand Response.Write to include the book title associated with each author.
So, I modified Author.
vb to read:
Code:
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
<XmlRoot(ElementName:="title", _
Namespace:="http://example.books.com")> Public Class Title
<XmlElement(ElementName:="title")> Public Title As String
End Class
<XmlRoot(ElementName:="author", _
Namespace:="http://example.books.com")> Public Class Author
<XmlElement(ElementName:="first-name")> Public FirstName As String
<XmlElement(ElementName:="last-name")> Public LastName As String
End Class
And changed Default.aspx.
vb to read
Code:
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
'Create factory early
Dim factory As New XmlSerializerFactory
Dim settings As New XmlReaderSettings()
Dim nt As New NameTable()
Dim book As Object = nt.Add("book")
Dim title As Object = nt.Add("title")
Dim price As Object = nt.Add("price")
Dim author As Object = nt.Add("author")
settings.NameTable = nt
Dim booksSchemaFile As String = Path.Combine(Request.PhysicalApplicationPath, "books.xsd")
settings.Schemas.Add(Nothing, XmlReader.Create(booksSchemaFile))
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, _
AddressOf settings_ValidationEventHandler
settings.IgnoreWhitespace = True
settings.IgnoreComments = True
Dim booksFile As String = _
Path.Combine(Request.PhysicalApplicationPath, "books.xml")
Using reader As XmlReader = XmlReader.Create(booksFile, settings)
While (reader.Read())
If (reader.NodeType = XmlNodeType.Element And author.Equals(reader.LocalName)) Then
'Then use the factory to create and cache serializers
Dim xs1 As XmlSerializer = factory.CreateSerializer(GetType(Title))
Dim a1 As Title = CType(xs1.Deserialize(reader), Title)
Response.Write(String.Format("Title, "))
Dim xs2 As XmlSerializer = factory.CreateSerializer(GetType(Author))
Dim a2 As Author = CType(xs2.Deserialize(reader.ReadSubtree), Author)
Response.Write(String.Format("Author: {1}, {0}<BR/>", _
a2.FirstName, a2.LastName))
End If
End While
End Using
End Sub
Sub settings_ValidationEventHandler(ByVal sender As Object, _
ByVal e As System.Xml.Schema.ValidationEventArgs)
Response.Write(e.Message)
End Sub
End Class
No error showed in VWD, so I ran Default.aspx in my browser. The result was the following error:
Quote:
Server Error in '/Listing10-10_app' Application.
<author xmlns='http://example.books.com'> was not expected.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: <author xmlns='http://example.books.com'> was not expected.
Source Error:
Line 41: 'Then use the factory to create and cache serializers
Line 42: Dim xs1 As XmlSerializer = factory.CreateSerializer(GetType(Title))
Line 43: Dim a1 As Title = CType(xs1.Deserialize(reader), Title)
Line 44: Response.Write(String.Format("Title, "))
Line 45: Dim xs2 As XmlSerializer = factory.CreateSerializer(GetType(Author))
Source File: C:\Websites\Autobanner\Professional\9780470187579\ Chapter 10\VB\Listing10-10_app\Default.aspx.vb Line: 43
Stack Trace:
[InvalidOperationException: <author xmlns='http://example.books.com'> was not expected.]
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReaderTitle.Read3_title() +118
Microsoft.Xml.Serialization.GeneratedAssembly.Titl eSerializer.Deserialize(XmlSerializationReader reader) +40
System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) +119
[InvalidOperationException: There is an error in XML document (8, 10).]
System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) +613
System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader) +30
_Default.Page_Load(Object sender, EventArgs e) in C:\Websites\Autobanner\Professional\9780470187579\ Chapter 10\VB\Listing10-10_app\Default.aspx.vb:43
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
|
What went wrong? What should I have written?