ASP.NET Tutorial/XML/XmlReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:57, 26 мая 2010
Содержание
- 1 Optimizing XmlReader with a NameTable (C#)
- 2 Optimizing XmlReader with a NameTable (VB)
- 3 Processing XML with an XmlReader (C#)
- 4 Processing XML with an XmlReader (VB)
- 5 Using XmlReader.ReadElementContentAs (C#)
- 6 Using XmlReader.ReadElementContentAs (VB)
- 7 Validating XML with an XmlReader against an XML Schema (C#)
- 8 Validating XML with an XmlReader against an XML Schema (VB)
Optimizing XmlReader with a NameTable (C#)
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Xml; using System.Xml.Schema; public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { int bookcount = 0; XmlReaderSettings settings = new XmlReaderSettings(); NameTable nt = new NameTable(); object book = nt.Add("book"); settings.NameTable = nt; string booksSchemaFile = Path.rubine(Request.PhysicalApplicationPath, "Data.xsd"); settings.Schemas.Add(null, XmlReader.Create(booksSchemaFile)); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; string booksFile = Path.rubine(Request.PhysicalApplicationPath, "Data.xml"); using (XmlReader reader = XmlReader.Create(booksFile, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && book.Equals(reader.LocalName)) { bookcount++; } } } Response.Write(String.Format("Found {0} books!", bookcount)); } void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { Response.Write(e.Message); }
} File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore> File: Data.xsd <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.ru" xmlns="http://example.books.ru" targetNamespace="http://example.books.ru" elementFormDefault="qualified">
<xsd:element name="bookstore" type="bookstoreType" /> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string" /> <xsd:element name="author" type="authorName" /> <xsd:element name="price" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string" /> <xsd:attribute name="publicationdate" type="xsd:string" /> <xsd:attribute name="ISBN" type="xsd:string" /> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string" /> <xsd:element name="last-name" type="xsd:string" /> </xsd:sequence> </xsd:complexType>
</xsd:schema></source>
Optimizing XmlReader with a NameTable (VB)
<source lang="csharp">
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.vb Imports System.IO Imports System.Xml Imports System.Xml.Schema Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim bookcount As Integer = 0 Dim settings As New XmlReaderSettings() Dim nt As New NameTable() Dim book As Object = nt.Add("book") settings.NameTable = nt Dim booksSchemaFile As String = Path.rubine(Request.PhysicalApplicationPath, "Data.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.rubine(Request.PhysicalApplicationPath, "Data.xml") Using reader As XmlReader = XmlReader.Create(booksFile, settings) While (reader.Read()) If (reader.NodeType = XmlNodeType.Element _ And book.Equals(reader.LocalName)) Then bookcount += 1 End If End While End Using Response.Write(String.Format("Found {0} books!", bookcount)) End Sub Sub settings_ValidationEventHandler(ByVal sender As Object, _ ByVal e As System.Xml.Schema.ValidationEventArgs) Response.Write(e.Message) End Sub
End Class File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore> File: Data.xsd <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.ru" xmlns="http://example.books.ru" targetNamespace="http://example.books.ru" elementFormDefault="qualified">
<xsd:element name="bookstore" type="bookstoreType" /> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string" /> <xsd:element name="author" type="authorName" /> <xsd:element name="price" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string" /> <xsd:attribute name="publicationdate" type="xsd:string" /> <xsd:attribute name="ISBN" type="xsd:string" /> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string" /> <xsd:element name="last-name" type="xsd:string" /> </xsd:sequence> </xsd:complexType>
</xsd:schema></source>
Processing XML with an XmlReader (C#)
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Xml; public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { int bookcount = 0; XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; string booksFile = Path.rubine(Request.PhysicalApplicationPath, "Data.xml"); using (XmlReader reader = XmlReader.Create(booksFile, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && "book" == reader.LocalName) { bookcount++; } } } Response.Write(String.Format("Found {0} books!", bookcount)); }
} File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore></source>
Processing XML with an XmlReader (VB)
<source lang="csharp">
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.vb Imports System.IO Imports System.Xml Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim bookcount As Integer = 0 Dim settings As New XmlReaderSettings() settings.IgnoreWhitespace = True settings.IgnoreComments = True Dim booksFile As String = _ Path.rubine(Request.PhysicalApplicationPath, "Data.xml") Using reader As XmlReader = XmlReader.Create(booksFile, settings) While (reader.Read()) If (reader.NodeType = XmlNodeType.Element _ And "book" = reader.LocalName) Then bookcount += 1 End If End While End Using Response.Write(String.Format("Found {0} books!", bookcount)) End Sub
End Class File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore></source>
Using XmlReader.ReadElementContentAs (C#)
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Xml; using System.Xml.Schema; public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { int bookcount = 0; decimal booktotal = 0; XmlReaderSettings settings = new XmlReaderSettings(); NameTable nt = new NameTable(); object book = nt.Add("book"); object price = nt.Add("price"); settings.NameTable = nt; string booksSchemaFile = Path.rubine(Request.PhysicalApplicationPath, "Data.xsd"); settings.Schemas.Add(null, XmlReader.Create(booksSchemaFile)); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; string booksFile = Path.rubine(Request.PhysicalApplicationPath, "Data.xml"); using (XmlReader reader = XmlReader.Create(booksFile, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && book.Equals(reader.LocalName)) { bookcount++; } if (reader.NodeType == XmlNodeType.Element && price.Equals(reader.LocalName)) { booktotal += (decimal)reader.ReadElementContentAsObject(); } } } Response.Write(String.Format("Found {0} books that total {1:C}!", bookcount, booktotal)); } void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { Response.Write(e.Message); }
} File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore> File: Data.xsd <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.ru" xmlns="http://example.books.ru" targetNamespace="http://example.books.ru" elementFormDefault="qualified">
<xsd:element name="bookstore" type="bookstoreType" /> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string" /> <xsd:element name="author" type="authorName" /> <xsd:element name="price" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string" /> <xsd:attribute name="publicationdate" type="xsd:string" /> <xsd:attribute name="ISBN" type="xsd:string" /> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string" /> <xsd:element name="last-name" type="xsd:string" /> </xsd:sequence> </xsd:complexType>
</xsd:schema></source>
Using XmlReader.ReadElementContentAs (VB)
<source lang="csharp">
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.vb Imports System.IO Imports System.Xml Imports System.Xml.Schema Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim bookcount As Integer = 0 Dim booktotal As Decimal = 0 Dim settings As New XmlReaderSettings() Dim nt As New NameTable() Dim book As Object = nt.Add("book") Dim price As Object = nt.Add("price") settings.NameTable = nt Dim booksSchemaFile As String = Path.rubine(Request.PhysicalApplicationPath, "Data.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.rubine(Request.PhysicalApplicationPath, "Data.xml") Using reader As XmlReader = XmlReader.Create(booksFile, settings) While (reader.Read()) If (reader.NodeType = XmlNodeType.Element _ And book.Equals(reader.LocalName)) Then "A subtle, but significant change! bookcount += 1 End If If (reader.NodeType = XmlNodeType.Element And price.Equals(reader.LocalName)) Then booktotal += CType(reader.ReadElementContentAsObject(), Decimal) End If End While End Using Response.Write(String.Format("Found {0} books that total {1:C}!", _ bookcount, booktotal)) End Sub Sub settings_ValidationEventHandler(ByVal sender As Object, _ ByVal e As System.Xml.Schema.ValidationEventArgs) Response.Write(e.Message) End Sub
End Class File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore> File: Data.xsd <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.ru" xmlns="http://example.books.ru" targetNamespace="http://example.books.ru" elementFormDefault="qualified">
<xsd:element name="bookstore" type="bookstoreType" /> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string" /> <xsd:element name="author" type="authorName" /> <xsd:element name="price" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string" /> <xsd:attribute name="publicationdate" type="xsd:string" /> <xsd:attribute name="ISBN" type="xsd:string" /> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string" /> <xsd:element name="last-name" type="xsd:string" /> </xsd:sequence> </xsd:complexType>
</xsd:schema></source>
Validating XML with an XmlReader against an XML Schema (C#)
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Xml; using System.Xml.Schema; public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { int bookcount = 0; XmlReaderSettings settings = new XmlReaderSettings(); string booksSchemaFile = Path.rubine(Request.PhysicalApplicationPath, "Data.xsd"); settings.Schemas.Add(null, XmlReader.Create(booksSchemaFile)); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; string booksFile = Path.rubine(Request.PhysicalApplicationPath, "Data.xml"); using (XmlReader reader = XmlReader.Create(booksFile, settings)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && "book" == reader.LocalName) { bookcount++; } } } Response.Write(String.Format("Found {0} books!", bookcount)); } void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { Response.Write(e.Message); }
} File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore> File: Data.xsd <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.ru" xmlns="http://example.books.ru" targetNamespace="http://example.books.ru" elementFormDefault="qualified">
<xsd:element name="bookstore" type="bookstoreType" /> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string" /> <xsd:element name="author" type="authorName" /> <xsd:element name="price" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string" /> <xsd:attribute name="publicationdate" type="xsd:string" /> <xsd:attribute name="ISBN" type="xsd:string" /> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string" /> <xsd:element name="last-name" type="xsd:string" /> </xsd:sequence> </xsd:complexType>
</xsd:schema></source>
Validating XML with an XmlReader against an XML Schema (VB)
<source lang="csharp">
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
</form>
</body> </html> File: Default.aspx.vb Imports System.IO Imports System.Xml Imports System.Xml.Schema Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim bookcount As Integer = 0 Dim settings As New XmlReaderSettings() Dim booksSchemaFile As String = Path.rubine(Request.PhysicalApplicationPath, "Data.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.rubine(Request.PhysicalApplicationPath, "Data.xml") Using reader As XmlReader = XmlReader.Create(booksFile, settings) While (reader.Read()) If (reader.NodeType = XmlNodeType.Element And "book" = reader.LocalName) Then bookcount += 1 End If End While End Using Response.Write(String.Format("Found {0} books!", bookcount)) End Sub Sub settings_ValidationEventHandler(ByVal sender As Object, _ ByVal e As System.Xml.Schema.ValidationEventArgs) Response.Write(e.Message) End Sub
End Class
File: Data.xml <?xml version="1.0"?> <bookstore xmlns="http://example.books.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book genre="A" publicationdate="1981" ISBN="1-11111-11-0"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>8</price> </book> <book genre="B" publicationdate="1999" ISBN="0-222-22222-2"> <title>title 2</title> <author> <first-name>C</first-name> <last-name>D</last-name> </author> <price>11.99</price> </book>
</bookstore> File: Data.xsd <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.ru" xmlns="http://example.books.ru" targetNamespace="http://example.books.ru" elementFormDefault="qualified">
<xsd:element name="bookstore" type="bookstoreType" /> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string" /> <xsd:element name="author" type="authorName" /> <xsd:element name="price" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string" /> <xsd:attribute name="publicationdate" type="xsd:string" /> <xsd:attribute name="ISBN" type="xsd:string" /> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string" /> <xsd:element name="last-name" type="xsd:string" /> </xsd:sequence> </xsd:complexType>
</xsd:schema></source>