ReadTypedValue() public method

public ReadTypedValue ( ) : object
return object
        protected void Page_Load(object sender, EventArgs e)
        {
            string xmlFile = Server.MapPath("~/Customers1.xml");
            string xsdFile = Server.MapPath("~/Customers.xsd");

            XmlTextReader textReader = new XmlTextReader(xmlFile);
            XmlValidatingReader validatingReader = new XmlValidatingReader(textReader);
            validatingReader.Schemas.Add(null, xsdFile);
            validatingReader.ValidationType = ValidationType.Schema;
            validatingReader.ValidationEventHandler += new ValidationEventHandler(validatingReader_ValidationEventHandler);

            while (validatingReader.Read())
            {
                if (validatingReader.NodeType == XmlNodeType.Element)
                {
                    if (validatingReader.SchemaType is XmlSchemaComplexType)
                    {
                        XmlSchemaComplexType complexType = (XmlSchemaComplexType)validatingReader.SchemaType;
                        Response.Write(validatingReader.Name + " " + complexType.Name);
                    }
                    else
                    {
                        object innerText = validatingReader.ReadTypedValue();
                        Response.Write(validatingReader.Name + " : " + innerText.ToString() + " <br />");
                    }
                }
            }
            validatingReader.Close();
        }
		public void TestReadTypedValueSimple ()
		{
			string xml = "<root>12</root>";
			XmlSchema schema = new XmlSchema ();
			XmlSchemaElement elem = new XmlSchemaElement ();
			elem.Name = "root";
			elem.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
			schema.Items.Add (elem);

			// Lap 1:
			
			xvr = PrepareXmlReader (xml);
			xvr.Schemas.Add (schema);
			// Read directly from root.
			object o = xvr.ReadTypedValue ();
			Assert.AreEqual (ReadState.Initial, xvr.ReadState);
			Assert.IsNull (o);

			xvr.Read ();	// element root
			Assert.AreEqual (XmlNodeType.Element, xvr.NodeType);
			Assert.IsNotNull (xvr.SchemaType);
			Assert.IsTrue (xvr.SchemaType is XmlSchemaDatatype);
			o = xvr.ReadTypedValue ();	// read "12"
			Assert.AreEqual (XmlNodeType.EndElement, xvr.NodeType);
			Assert.IsNotNull (o);
			Assert.AreEqual (typeof (decimal), o.GetType ());
			decimal n = (decimal) o;
			Assert.AreEqual (12, n);
			Assert.IsTrue (!xvr.EOF);
			Assert.AreEqual ("root", xvr.Name);
			Assert.IsNull (xvr.SchemaType);	// EndElement's type

			// Lap 2:

			xvr = PrepareXmlReader (xml);
			xvr.Schemas.Add (schema);
			xvr.Read ();	// root
			XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
			Assert.IsNotNull (dt);
			Assert.AreEqual (typeof (decimal), dt.ValueType);
			Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType);
			xvr.Read ();	// text "12"
			Assert.IsNull (xvr.SchemaType);
			o = xvr.ReadTypedValue ();
			// ReadTypedValue is different from ReadString().
			Assert.IsNull (o);
		}
		public void ReadTypedValueWhitespaces ()
		{
			string xml = "<root>  </root><!-- after -->";
			string xsd = @"
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  <xs:element name='root'>
    <xs:simpleType>
      <xs:restriction base='xs:string'>
        <xs:minLength value='2' />
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>";
			XmlTextReader xir = 
				new XmlTextReader (xml, XmlNodeType.Document, null);
			XmlTextReader xsr =
				new XmlTextReader (xsd, XmlNodeType.Document, null);
			XmlValidatingReader vr = new XmlValidatingReader (xir);
			vr.Schemas.Add (XmlSchema.Read (xsr, null));
			vr.Read (); // root
			Assert.AreEqual ("  ", vr.ReadTypedValue ());
			Assert.AreEqual (XmlNodeType.EndElement, vr.NodeType);
		}
		public void TestReadTypedAttributeValue ()
		{
			string xml = "<root attr='12'></root>";
			XmlSchema schema = new XmlSchema ();
			XmlSchemaElement elem = new XmlSchemaElement ();
			elem.Name = "root";
			XmlSchemaComplexType ct = new XmlSchemaComplexType ();
			XmlSchemaAttribute attr = new XmlSchemaAttribute ();
			attr.Name = "attr";
			attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
			ct.Attributes.Add (attr);
			elem.SchemaType = ct;
			schema.Items.Add (elem);

			xvr = PrepareXmlReader (xml);
			xvr.Schemas.Add (schema);
			xvr.Read ();
			Assert.AreEqual ("root", xvr.Name);
			Assert.IsTrue (xvr.MoveToNextAttribute ());	// attr
			Assert.AreEqual ("attr", xvr.Name);
			XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
			Assert.IsNotNull (dt);
			Assert.AreEqual (typeof (int), dt.ValueType);
			Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType);
			object o = xvr.ReadTypedValue ();
			Assert.AreEqual (XmlNodeType.Attribute, xvr.NodeType);
			Assert.AreEqual (typeof (int), o.GetType ());
			int n = (int) o;
			Assert.AreEqual (12, n);
			Assert.IsTrue (xvr.ReadAttributeValue ());	// can read = seems not proceed.
		}
		public override object Parse (string value, XmlReader reader)
		{
			// Now we create XmlValidatingReader to handle
			// simple-type based validation (since there is no
			// other way, because of sucky XmlSchemaSimpleType
			// design).
			if (value != null)
				value = value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
			XmlValidatingReader v = new XmlValidatingReader (
				new XmlTextReader (
					String.Concat ("<root>", value, "</root>"),
					XmlNodeType.Document,
					null));
			v.Schemas.Add (schema);
			v.Read (); // <root>
			try {
				return v.ReadTypedValue ();
			} finally {
				v.Read (); // </root>
			}
		}