Reading author instances from an XmlReader using XmlSerialization (C#) : XmlSerialization « XML « ASP.NET Tutorial






<%@ 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">
    <div>
    
    </div>
    </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;
using System.Xml.Serialization;



[XmlRoot(ElementName = "author", Namespace = "http://example.books.com")]
public class Author
{
    [XmlElement(ElementName = "first-name")]
    public string FirstName;

    [XmlElement(ElementName = "last-name")]
    public string LastName;
}


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlSerializerFactory factory = new XmlSerializerFactory();
        
        XmlReaderSettings settings = new XmlReaderSettings();

        NameTable nt = new NameTable();
        object book = nt.Add("book");
        object price = nt.Add("price");
        object author = nt.Add("author");
        settings.NameTable = nt;

        string booksSchemaFile = Path.Combine(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.Combine(Request.PhysicalApplicationPath, "Data.xml");
        using (XmlReader reader = XmlReader.Create(booksFile, settings))
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && author.Equals(reader.LocalName))
                {
                    XmlSerializer xs = factory.CreateSerializer(typeof(Author));
                    Author a = (Author)xs.Deserialize(reader.ReadSubtree());
                    Response.Write(String.Format("Author: {1}, {0}<BR/>",
                        a.FirstName, a.LastName));
                }
            }
        }
    }

    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.com"
           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.com" xmlns="http://example.books.com" targetNamespace="http://example.books.com" 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>








25.14.XmlSerialization
25.14.1.Reading author instances from an XmlReader using XmlSerialization (C#)
25.14.2.Reading author instances from an XmlReader using XmlSerialization (VB)