Пример #1
0
        static void Main(string[] args)
        {
            // Create a cache of schemas, and add two schemas
            XmlSchemaCollection sc = new XmlSchemaCollection();

            sc.Add("urn:MyUri", "../../../doctors.xsd");
            //sc.Add("", "../../../doctors.xsd");

            // Create a validating reader object
            XmlTextReader       tr = new XmlTextReader("../../../doctors.xml");
            XmlValidatingReader vr = new XmlValidatingReader(tr);

            // Specify the type of validation required
            vr.ValidationType = ValidationType.Schema;

            // Tell the validating reader to use the schema collection
            vr.Schemas.Add(sc);

            // Register a validation event handler method
            vr.ValidationEventHandler += new ValidationEventHandler(MyHandler);

            // Read and validate the XML document
            try
            {
                int   num     = 0;
                float avg_age = 0;
                while (vr.Read())
                {
                    if (vr.NodeType == XmlNodeType.Element &&
                        vr.LocalName == "P")
                    {
                        num++;

                        vr.MoveToFirstAttribute();
                        Console.WriteLine(vr.Value);
                        vr.MoveToNextAttribute();
                        vr.MoveToNextAttribute();
                        string val = vr.Value;
                        if (val != "male" && val != "female")
                        {
                            //Console.WriteLine(val);
                            avg_age += Convert.ToInt32(vr.Value);
                        }

                        vr.MoveToElement();
                    }
                }

                Console.WriteLine("Number of Passengers: " + num + "\n");
                Console.WriteLine("Average age: " + avg_age / num + "\n");
            }
            catch (XmlException ex)
            {
                Console.WriteLine("XMLException occurred: " + ex.Message);
            }
            finally
            {
                vr.Close();
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            XmlSchemaCollection sc = new XmlSchemaCollection();

            sc.Add("", "../../../../visual_studio_schema_test.xsd");

            // Create a validating reader object
            XmlTextReader tr = new XmlTextReader("../../../../test_xml.xml");
            //XmlTextReader tr = new XmlTextReader("../../../../xml_test_wrong_atrib.xml");
            //XmlTextReader tr = new XmlTextReader("../../../../xml_test_wrong_root.xml");
            XmlValidatingReader vr = new XmlValidatingReader(tr);

            // Specify the type of validation required
            vr.ValidationType = ValidationType.Schema;

            // Tell the validating reader to use the schema collection
            vr.Schemas.Add(sc);

            // Register a validation event handler method
            vr.ValidationEventHandler += new ValidationEventHandler(MyHandler);

            // Read and validate the XML document
            try
            {
                int num = 0;
                while (vr.Read())
                {
                    if (vr.NodeType == XmlNodeType.Element &&
                        vr.LocalName == "Games")
                    {
                        num++;

                        vr.MoveToFirstAttribute();
                        Console.WriteLine(vr.Value);
                        vr.MoveToNextAttribute();
                        Console.WriteLine(vr.Value);
                        vr.MoveToNextAttribute();
                        Console.WriteLine(vr.Value);
                        vr.MoveToNextAttribute();
                        Console.WriteLine(vr.Value);
                        vr.MoveToNextAttribute();
                        Console.WriteLine(vr.Value);

                        vr.MoveToElement();
                    }
                }

                Console.WriteLine("Number of strings: " + num + "\n");
            }
            catch (XmlException ex)
            {
                Console.WriteLine("XMLException occurred: " + ex.Message);
            }
            finally
            {
                vr.Close();
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            // Create a cache of schemas, and add two schemas
            XmlSchemaCollection sc = new XmlSchemaCollection();

            sc.Add("", "D:/university/database/mssql/lab5/task1XSDGENERATOR.xsd");
            //sc.Add("", "../../../doctors.xsd");

            // Create a validating reader object
            XmlTextReader       tr = new XmlTextReader("D:/university/database/mssql/lab5/task1.xml");
            XmlValidatingReader vr = new XmlValidatingReader(tr);

            // Specify the type of validation required
            vr.ValidationType = ValidationType.Schema;

            // Tell the validating reader to use the schema collection
            vr.Schemas.Add(sc);

            // Register a validation event handler method
            vr.ValidationEventHandler += new ValidationEventHandler(MyHandler);

            // Read and validate the XML document
            try
            {
                int num = 0;
                while (vr.Read())
                {
                    if (vr.NodeType == XmlNodeType.Element &&
                        vr.LocalName == "C")
                    {
                        num++;
                        vr.MoveToElement();
                    }
                }

                Console.WriteLine("Number of Clients: " + num + "\n");
            }
            catch (XmlException ex)
            {
                Console.WriteLine("XMLException occurred: " + ex.Message);
            }
            finally
            {
                vr.Close();
            }
            Console.Read();
        }
Пример #4
0
    public static void Main()
    {
        XmlValidatingReader reader = null;

        try
        {
            //Create the string to parse.
            string xmlFrag = "<book genre='novel' ISBN='1-861003-78' pubdate='1987'></book> ";

            //Create the XmlNamespaceManager.
            NameTable           nt    = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

            //Create the XmlParserContext.
            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

            //Create the XmlValidatingReader .
            reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);

            //Read the attributes on the root element.
            reader.MoveToContent();
            if (reader.HasAttributes)
            {
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToAttribute(i);
                    Console.WriteLine("{0} = {1}", reader.Name, reader.Value);
                }
                //Move the reader back to the node that owns the attribute.
                reader.MoveToElement();
            }
        }

        finally
        {
            if (reader != null)
            {
                reader.Close();
            }
        }
    }