Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9
This is the forum to discuss the Wrox book Professional ASP.NET 3.5: In C# and VB by Bill Evjen, Scott Hanselman, Devin Rader; ISBN: 9780470187579
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 17th, 2009, 08:11 PM
Authorized User
 
Join Date: Jun 2009
Posts: 70
Thanks: 18
Thanked 1 Time in 1 Post
Default Chapter 10, listing 10-10-app

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?
  #2 (permalink)  
Old August 18th, 2009, 05:02 AM
Authorized User
 
Join Date: Jun 2009
Posts: 70
Thanks: 18
Thanked 1 Time in 1 Post
Default

I went on to change
Code:
 
If (reader.NodeType = XmlNodeType.Element And author.Equals(reader.LocalName)) Then
to read

Code:
 If (reader.NodeType = XmlNodeType.Element And author.Equals(reader.LocalName) And title.Equals(reader.LocalName)) Then
.

The result is no errors shown - but no result either. Just a blank screen. I think the problem lies in this line - and am attempting to debug. But I'd be grateful for a steer on this.
  #3 (permalink)  
Old August 18th, 2009, 05:21 AM
Authorized User
 
Join Date: Jun 2009
Posts: 70
Thanks: 18
Thanked 1 Time in 1 Post
Default

This doesn't cut it

Code:
If (reader.NodeType = XmlNodeType.Element And title.Equals(reader.LocalName) AndAlso XmlNodeType.Element And author.Equals(reader.LocalName)) Then
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Listing 8-10 and Listing 8-16. Asp.Net BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 1 February 6th, 2008 01:11 PM
Chapter 10 gogeo BOOK: Beginning Access 2003 VBA 1 January 22nd, 2006 09:41 AM
Chapter 10 czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 March 29th, 2005 09:35 AM
Ch. 10 Directory listing superkooi BOOK: Beginning ASP 3.0 1 March 3rd, 2005 07:39 AM
Chapter 10 columbiasmiles JSP Basics 0 May 17th, 2004 09:09 PM



All times are GMT -4. The time now is 04:30 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.