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);
        }
Example #2
0
    public static void Main()
    {
        XmlTextReader       tr = new XmlTextReader("booksSchema.xml");
        XmlValidatingReader vr = new XmlValidatingReader(tr);

        vr.Schemas.Add(null, "books.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;
                    Console.WriteLine("{0}({1})", vr.Name, sct.Name);
                }
                else
                {
                    object value = vr.ReadTypedValue();
                    Console.WriteLine("{0}({1}):{2}", vr.Name, value.GetType().Name, value);
                }
            }
        }
    }
Example #3
0
 public static void PrintTypeInfo(XmlValidatingReader vr)
 {
     if (vr.SchemaType != null)
     {
         if (vr.SchemaType is XmlSchemaDatatype || vr.SchemaType is XmlSchemaSimpleType)
         {
             object value = vr.ReadTypedValue();
             Console.WriteLine("{0}({1},{2}):{3}", vr.NodeType, vr.Name, value.GetType().Name, value);
         }
         else if (vr.SchemaType is XmlSchemaComplexType)
         {
             XmlSchemaComplexType sct = (XmlSchemaComplexType)vr.SchemaType;
             Console.WriteLine("{0}({1},{2})", vr.NodeType, vr.Name, sct.Name);
         }
     }
 }
Example #4
0
        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).
            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>
            }
        }
        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.
        }
Example #7
0
        private void ReadXML()
        {
            double   RelayDelay;
            DateTime date;
            string   IP;
            MODE     mode;
            decimal  TZ_Offset;
            string   Pword;

            try
            {
                //open up a stream and fed it to the XmlTextReader
                StreamReader  sRdr = new StreamReader(XMLfname);
                XmlTextReader tRdr = new XmlTextReader(sRdr);

                //Instantiate a new schemas collection
                //Add this one schema to the collection
                //A collection means that you can validate this XML file against any
                //number of schemas.  You would do this if you were reading the file
                //piecemeal
                XmlSchemaCollection Schemas = new XmlSchemaCollection();
                Schemas.Add(null, XSDfname);

                //Instantiate a new validating reader.  This validates for data type
                //and presence.
                //Add the schemas collection to the validating reader and funnel the
                //XmlTextReader through it.
                //wire up an ad-hoc validation delegate to catch any validation errors
                XmlValidatingReader vRdr = new XmlValidatingReader(tRdr);
                vRdr.ValidationType          = ValidationType.Schema;
                vRdr.ValidationEventHandler += new ValidationEventHandler(ValXML);
                vRdr.Schemas.Add(Schemas);

                //Read the XML file through the validator
                object node;
                while (vRdr.Read())
                {
                    node = null;
                    if (vRdr.LocalName.Equals("ConfigDate"))
                    {
                        node = vRdr.ReadTypedValue();
                        if (node != null)
                        {
                            date = (DateTime)node;
                        }
                    }
                    if (vRdr.LocalName.Equals("RelayDelay"))
                    {
                        node = vRdr.ReadTypedValue();
                        if (node != null)
                        {
                            RelayDelay = (double)node;
                        }
                    }
                    if (vRdr.LocalName.Equals("TimeZoneOffset"))
                    {
                        node = vRdr.ReadTypedValue();
                        if (node != null)
                        {
                            TZ_Offset = (decimal)node;
                        }
                    }
                    if (vRdr.LocalName.Equals("PassWord"))
                    {
                        node = vRdr.ReadTypedValue();
                        if (node != null)
                        {
                            Pword = (string)node;
                        }
                    }
                    if (vRdr.LocalName.Equals("Mode"))
                    {
                        node = vRdr.ReadTypedValue();
//            mode = (string)node;
                    }
                    if (vRdr.LocalName.Equals("IP"))
                    {
                        node = vRdr.ReadTypedValue();
                        if (node != null)
                        {
                            IP = (string)node;
                        }
                    }
                    if (node != null)
                    {
                        rcResults.AppendText(vRdr.LocalName + "\n");
                        rcResults.AppendText(node.GetType().ToString() + "\n");
                        rcResults.AppendText(node.ToString() + "\n\n");
                    }
                }
                vRdr.Close();
                tRdr.Close();
                sRdr.Close();
            }
            catch (Exception e)
            {
                //The handler will catch mal-formed xml docs.
                //It is not intended to catch bad data.  That is the delegates job
                MessageBox.Show("Exception analyzing Config file: " + e.Message);
            }
        }