Getting type information using xsd

serkan sendur 0 Tallied Votes 234 Views Share

To make some operations on XML elements you have to know their types. This snippet is about getting the types of these elements.

// this is default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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.Xml.Schema;
using System.Xml;
using System.Xml.XPath;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetTypes();
    }
    private  void GetTypes()
    {
        XmlTextReader tr = new XmlTextReader(Server.MapPath("~/App_Data/XMLFile.xml"));
        XmlValidatingReader vr = new XmlValidatingReader(tr);

        vr.Schemas.Add(null,Server.MapPath("~/App_Data/XMLFile.xsd"));
        vr.ValidationType = ValidationType.Schema;
        vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

        while (vr.Read())
        {
            if (vr.NodeType == XmlNodeType.Element)
            {
                if (vr.SchemaType is XmlSchemaComplexType)
                {
                    XmlSchemaComplexType sct = (XmlSchemaComplexType)vr.SchemaType;
                    Response.Write("<div style='color:red'>" + vr.Name + " " + sct.Name + "</div>");
                }
                else
                {
                    object value = vr.ReadTypedValue();
                    try
                    {
                    Response.Write("<div style='color:blue'>" + vr.Name + " : " + value.GetType().Name + " " + value.ToString() + "</div>");
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
    private  void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        Response.Write("***validation error</br>");
        Response.Write("Severity : " + args.Severity);
        Response.Write("Message : "+ args.Message);
    }
}
// this is default.aspx, it has basically nothing

<%@ 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>

// this is my xml file prototype
<?xml version="1.0" encoding="utf-8" ?>
<man>
  <length>180</length>
  <eyeColor>Green</eyeColor>
  <BOD>1981-08-10</BOD>
</man>

// this is my xsd file for my xml file
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="man">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="length" type="xs:int" />
        <xs:element name="eyeColor" type="xs:string" />
        <xs:element name="BOD" type="xs:boolean" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

// when you run the application the webpage displays the following 

man 
length : Int32 180
eyeColor : String Green
***validation error
Severity : ErrorMessage : The 'BOD' element has an invalid value according to its data type. 
it was able to get types from the xsd file and also validated the document against it.