Ejemplo n.º 1
0
  public static void Main()
  {
    XmlTextReader txtreader = null;
    XmlValidatingReader reader = null;

    try
    {
       //Implement the readers.
       txtreader = new XmlTextReader("elems.xml");
       reader = new XmlValidatingReader(txtreader);
  
       //Parse the XML and display the text content of each of the elements.
       while (reader.Read()){
         if (reader.IsStartElement()){
           if (reader.IsEmptyElement)
              Console.WriteLine("<{0}/>", reader.Name);
           else{
               Console.Write("<{0}> ", reader.Name);
               reader.Read(); //Read the start tag.
               if (reader.IsStartElement())  //Handle nested elements.
                   Console.Write("\r\n<{0}>", reader.Name);
               Console.WriteLine(reader.ReadString());  //Read the text content of the element.
           }
         }
       } 
       
     } 

     finally 
     {
        if (reader != null)
          reader.Close();
      }
  }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: AnalyzeTimesheets <XML file>");
                return;
            }

            try
            {
                // Code to read and validate XML goes here
                StreamReader        stream     = new StreamReader(args[0]);
                XmlTextReader       reader     = new XmlTextReader(stream);
                XmlSchemaCollection schemaColl = new XmlSchemaCollection();
                schemaColl.Add(null, "Timesheet.xsd");
                XmlValidatingReader valReader = new XmlValidatingReader(reader);
                valReader.ValidationType = ValidationType.Schema; // Q - types of validation?
                valReader.Schemas.Add(schemaColl);
                valReader.ValidationEventHandler += new ValidationEventHandler(valHandler);
                decimal hours = 0;
                while (valReader.Read())
                {
                    if (valReader.LocalName.Equals("ActivityName"))
                    {
                        Console.WriteLine("Activity: " + valReader.ReadString());
                    }
                    if (valReader.LocalName.Equals("ActivityDuration"))
                    {
                        hours += Decimal.Parse(valReader.ReadString());
                    }
                }
                Console.WriteLine("File processed. Number of hours worked - {0}", hours);
                valReader.Close();
                reader.Close();
                stream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception analyzing timesheet: " + e);
            }
        }
        public void TestExpandEntity()
        {
            string intSubset = "<!ELEMENT root (#PCDATA)><!ATTLIST root foo CDATA 'foo-def' bar CDATA 'bar-def'><!ENTITY ent 'entity string'>";
            string dtd       = "<!DOCTYPE root [" + intSubset + "]>";
            string xml       = dtd + "<root foo='&ent;' bar='internal &ent; value'>&ent;</root>";

            dvr = PrepareXmlReader(xml);
            dvr.EntityHandling = EntityHandling.ExpandEntities;
            dvr.Read();                 // DTD
            dvr.Read();
            Assert.AreEqual(XmlNodeType.Element, dvr.NodeType);
            Assert.AreEqual("root", dvr.Name);
            Assert.IsTrue(dvr.MoveToFirstAttribute());
            Assert.AreEqual("foo", dvr.Name);
            Assert.AreEqual("entity string", dvr.Value);
            Assert.IsTrue(dvr.MoveToNextAttribute());
            Assert.AreEqual("bar", dvr.Name);
            Assert.AreEqual("internal entity string value", dvr.Value);
            Assert.AreEqual("entity string", dvr.ReadString());

            // ValidationType = None

            dvr = PrepareXmlReader(xml);
            dvr.EntityHandling = EntityHandling.ExpandEntities;
            dvr.ValidationType = ValidationType.None;
            dvr.Read();                 // DTD
            dvr.Read();
            Assert.AreEqual(XmlNodeType.Element, dvr.NodeType);
            Assert.AreEqual("root", dvr.Name);

            Assert.IsTrue(dvr.MoveToFirstAttribute());
            Assert.AreEqual("foo", dvr.Name);
            Assert.AreEqual("entity string", dvr.Value);

            Assert.IsTrue(dvr.MoveToNextAttribute());
            Assert.AreEqual("bar", dvr.Name);
            Assert.AreEqual("internal entity string value", dvr.Value);
            Assert.AreEqual("entity string", dvr.ReadString());
        }
Ejemplo n.º 4
0
        /*Function to validate the input document*/
        public void ValidateOutputFile(String outFile)
        {
            isValid = true;
            XmlTextReader       xml = new XmlTextReader(outFile);
            XmlValidatingReader xsd = new XmlValidatingReader(xml);

            try {
                xsd.ValidationType          = ValidationType.DTD;
                xsd.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);

                while (xsd.Read())
                {
                    errorText = xsd.ReadString();
                    if (errorText.Length > 100)
                    {
                        errorText = errorText.Substring(0, 100);
                    }
                }
                xsd.Close();

                Stream   stream = null;
                Assembly asm    = Assembly.GetExecutingAssembly();
                foreach (string name in asm.GetManifestResourceNames())
                {
                    if (name.EndsWith("Shematron.xsl"))
                    {
                        stream = asm.GetManifestResourceStream(name);
                        break;
                    }
                }

                XmlReader     rdr = XmlReader.Create(stream);
                XPathDocument doc = new XPathDocument(outFile);

                XslCompiledTransform trans = new XslCompiledTransform(false);
                trans.Load(rdr);

                XmlTextWriter myWriter = new XmlTextWriter(Path.GetDirectoryName(outFile) + "\\report.txt", null);
                trans.Transform(doc, null, myWriter);

                myWriter.Close();
                rdr.Close();

                StreamReader reader = new StreamReader(Path.GetDirectoryName(outFile) + "\\report.txt");
                if (!reader.EndOfStream)
                {
                    error  += reader.ReadToEnd();
                    isValid = false;
                }
                reader.Close();

                if (File.Exists(Path.GetDirectoryName(outFile) + "\\report.txt"))
                {
                    File.Delete(Path.GetDirectoryName(outFile) + "\\report.txt");
                }

                // Check whether the document is valid or invalid.
                if (isValid == false)
                {
                    if (error_MasterSub != "")
                    {
                        error_Exception = "Translation failed." + "\n" + "Validation error found while translating the following documents" + "\n" + " \"" + error_MasterSub + "\n" + error;
                    }
                    else
                    {
                        error_Exception = "Translated document is invalid." + "\n\n" + error;
                    }
                }
                else
                {
                    if (error_MasterSub != "")
                    {
                        error_Exception = "Translation failed." + "\n\n" + "Validation error found while translating the following documents" + "\n" + " \"" + error_MasterSub + "\n" + error;
                    }
                }
            } catch (UnauthorizedAccessException a) {
                xsd.Close();
                //dont have access permission
                String error = a.Message;
                report.AddLog("", "Validation Error of translated DAISY File: \n" + error, Report.ERROR_LEVEL);
            } catch (Exception a) {
                xsd.Close();
                //and other things that could go wrong
                String error = a.Message;
                report.AddLog("", "Validation Error of translated DAISY File: \n" + error, Report.ERROR_LEVEL);
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            // Der Dateiname der XML- und der Schema-Datei
            string xmlFileName       = Path.Combine(Application.StartupPath, "Persons.xml");
            string xmlSchemaFileName = Path.Combine(Application.StartupPath,
                                                    "Persons.xsd");

            // XmlValidatingReader mit einem neuen XmlReader als Basis erzeugen
            XmlValidatingReader xmlValidator = new XmlValidatingReader(
                new XmlTextReader(xmlFileName));

            // Validier-Typ auf "Schema" festlegen
            xmlValidator.ValidationType = ValidationType.Schema;

            // Schema ohne Ziel-Namensraum hinzufügen
            try
            {
                xmlValidator.Schemas.Add(null, xmlSchemaFileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            // Merker für die Gültigkeit des Dokuments initialisieren
            bool isValid = true;

            try
            {
                // Daten einlesen
                while (xmlValidator.Read())
                {
                    // Überprüfen, ob der aktuelle Knoten ein Element ist
                    if (xmlValidator.NodeType == XmlNodeType.Element)
                    {
                        // Überprüfen, um welches Element es sich handelt, und
                        // die eingelesenen Daten entsprechend ausgeben
                        if (xmlValidator.Name == "person")
                        {
                            Console.WriteLine();
                            Console.WriteLine("Person {0}",
                                              xmlValidator.GetAttribute("id"));
                        }
                        else if (xmlValidator.Name == "firstname")
                        {
                            if (xmlValidator.MoveToContent() != XmlNodeType.None)
                            {
                                Console.WriteLine("Vorname: {0}",
                                                  xmlValidator.ReadString());
                            }
                        }
                        else if (xmlValidator.Name == "lastname")
                        {
                            if (xmlValidator.MoveToContent() != XmlNodeType.None)
                            {
                                Console.WriteLine("Nachname: {0}",
                                                  xmlValidator.ReadString());
                            }
                        }
                        else if (xmlValidator.Name == "type")
                        {
                            if (xmlValidator.MoveToContent() != XmlNodeType.None)
                            {
                                Console.WriteLine("Typ: {0}",
                                                  xmlValidator.ReadString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Fehler beim Lesen: Fehler ausgeben und Merker für die Gültigkeit des
                // Dokuments zurücksetzen
                Console.WriteLine(ex.Message);
                isValid = false;
            }

            // XmlValidatingReader schließen
            xmlValidator.Close();

            // Ausgeben, ob die Datei gültig ist
            Console.WriteLine();
            if (isValid)
            {
                Console.WriteLine("Die XML-Datei ist gültig");
            }
            else
            {
                Console.WriteLine("Die XML-Datei ist nicht gültig");
            }

            Console.WriteLine("Beenden mit Return");
            Console.ReadLine();
        }
Ejemplo n.º 6
0
        /*Function to do validation of Output XML file with DTD*/
        public void XmlValidation(String outFile)
        {
            isValid = true;
            error   = "";
            XmlTextReader       xml = new XmlTextReader(outFile);
            XmlValidatingReader xsd = new XmlValidatingReader(xml);

            try
            {
                xsd.ValidationType = ValidationType.DTD;
                ArrayList errTxt = new ArrayList();
                for (int i = 0; i <= 4; i++)
                {
                    errTxt.Add("");
                }
                xsd.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
                while (xsd.Read())
                {
                    errTxt[4] = errTxt[3];
                    errTxt[3] = errTxt[2];
                    errTxt[2] = errTxt[1];
                    errTxt[1] = errTxt[0];
                    errTxt[0] = xsd.ReadString();
                    errorText = "";
                    for (int i = 4; i >= 0; i--)
                    {
                        errorText = errorText + errTxt[i].ToString() + " ";
                    }
                    if (errorText.Contains("\n"))
                    {
                        errorText = errorText.Replace("\n", "");
                    }
                    if (errorText.Contains("\r"))
                    {
                        errorText = errorText.Replace("\r", "");
                    }
                    if (errorText.Length > 100)
                    {
                        errorText = errorText.Substring(0, 100);
                    }
                }
                xsd.Close();

                Stream   stream = null;
                Assembly asm    = Assembly.GetExecutingAssembly();
                foreach (string name in asm.GetManifestResourceNames())
                {
                    if (name.EndsWith("Shematron.xsl"))
                    {
                        stream = asm.GetManifestResourceStream(name);
                        break;
                    }
                }

                XmlReader     rdr = XmlReader.Create(stream);
                XPathDocument doc = new XPathDocument(outFile);

                XslCompiledTransform trans = new XslCompiledTransform(true);
                trans.Load(rdr);

                XmlTextWriter myWriter = new XmlTextWriter(Path.GetDirectoryName(outFile) + "\\report.txt", null);
                trans.Transform(doc, null, myWriter);

                myWriter.Close();
                rdr.Close();

                StreamReader reader = new StreamReader(Path.GetDirectoryName(outFile) + "\\report.txt");
                if (!reader.EndOfStream)
                {
                    error += reader.ReadToEnd();

                    if (feedbackValidationIntercepted != null)
                    {
                        feedbackValidationIntercepted(this, new DaisyEventArgs(error));
                    }
                }
                reader.Close();

                if (File.Exists(Path.GetDirectoryName(outFile) + "\\report.txt"))
                {
                    File.Delete(Path.GetDirectoryName(outFile) + "\\report.txt");
                }

                // Check whether the document is valid or invalid.
                if (isValid == false)
                {
                    if (feedbackValidationIntercepted != null)
                    {
                        feedbackValidationIntercepted(this, new DaisyEventArgs(error));
                    }
                }
            }
            catch (UnauthorizedAccessException a)
            {
                xsd.Close();
                //dont have access permission
                error = a.Message;

                if (feedbackValidationIntercepted != null)
                {
                    feedbackValidationIntercepted(this, new DaisyEventArgs(error));
                }
            }
            catch (Exception a)
            {
                xsd.Close();
                //and other things that could go wrong
                error = a.Message;

                if (feedbackValidationIntercepted != null)
                {
                    feedbackValidationIntercepted(this, new DaisyEventArgs(error));
                }
            }
        }
Ejemplo n.º 7
0
        private void control_Load(object sender, System.EventArgs e)
        {
            if (ok == 1)
            {
                this.groupBox1.Enabled   = false;
                this.button1.Visible     = false;
                this.label9.Visible      = false;
                this.label10.Visible     = false;
                this.pictureBox1.Visible = false;
                this.label6.Text         = "  Capturing ...";
                this.label7.Text         = "  ON";
                this.label8.Text         = "To Disconnecting Press ";
                this.label12.Text        = " \"Ctrl+X\" ";
                this.Focus();
            }
            // READ FORM XML FILE
            try
            {
                emptyfile = false;
                StreamReader        stream     = new StreamReader("c:/robonet/XML/jobsinfo.xml");
                XmlTextReader       reader     = new XmlTextReader(stream);
                XmlSchemaCollection schemacoll = new XmlSchemaCollection();
                schemacoll.Add(null, "c:/robonet/XSD/jobsinfo.xsd");
                XmlValidatingReader valreader = new XmlValidatingReader(reader);
                valreader.ValidationType = ValidationType.Schema;
                valreader.Schemas.Add(schemacoll);
                valreader.ValidationEventHandler += new ValidationEventHandler(valHandler);
                while (valreader.Read())
                {
                    if (valreader.LocalName.Equals("jobname"))
                    {
                        jobnamefild[jobnamecounter] = valreader.ReadString();
                        jobnamecounter++;
                    }
                    else if (valreader.LocalName.Equals("jobpath"))
                    {
                        jobpathfild[jobpathcounter] = valreader.ReadString();
                        jobpathcounter++;
                    }
                }
                valreader.Close();
                reader.Close();
                stream.Close();
                // READING COMPLETE
            }
            catch (Exception caught)
            {
                emptyfile = true;
            }
            // FILL MENUITEMS
            int count;

            for (count = 0; jobnamefild[count] != null; count++)
            {
                ;
            }
            jobs  = new string[count];
            paths = new string[count];
            for (count = 0; jobnamefild[count] != null; count++)
            {
                jobs[count]  = jobnamefild[count];
                paths[count] = jobpathfild[count];
            }
            MJmenu myjobsname = new MJmenu();

            this.menuItem13.MenuItems.Add(myjobsname);
            myjobsname.Initialize(jobs);
            myjobsname.MenuClicked += new EventHandler(MenuClick);
            this.label13.Text       = "Current user : ( " + this.currentusername + " )";
        }
Ejemplo n.º 8
0
        /* Function which validated the master.xml*/
        public void XmlValidation(String outFile)
        {
            isValid = true;
            error   = "";
            XmlTextReader       xml = new XmlTextReader(outFile);
            XmlValidatingReader xsd = new XmlValidatingReader(xml);

            try
            {
                xsd.ValidationType          = ValidationType.DTD;
                xsd.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);

                while (xsd.Read())
                {
                    errorText = xsd.ReadString();
                    if (errorText.Length > 100)
                    {
                        errorText = errorText.Substring(0, 100);
                    }
                }
                xsd.Close();

                Stream   stream = null;
                Assembly asm    = Assembly.GetExecutingAssembly();
                foreach (string name in asm.GetManifestResourceNames())
                {
                    if (name.EndsWith("Shematron.xsl"))
                    {
                        stream = asm.GetManifestResourceStream(name);
                        break;
                    }
                }

                XmlReader     rdr = XmlReader.Create(stream);
                XPathDocument doc = new XPathDocument(outFile);

                XslCompiledTransform trans = new XslCompiledTransform(true);
                trans.Load(rdr);

                XmlTextWriter myWriter = new XmlTextWriter(Path.GetDirectoryName(outFile) + "\\report.txt", null);
                trans.Transform(doc, null, myWriter);

                myWriter.Close();
                rdr.Close();

                StreamReader reader = new StreamReader(Path.GetDirectoryName(outFile) + "\\report.txt");
                if (!reader.EndOfStream)
                {
                    error  += reader.ReadToEnd();
                    isValid = false;
                }
                reader.Close();

                if (File.Exists(Path.GetDirectoryName(outFile) + "\\report.txt"))
                {
                    File.Delete(Path.GetDirectoryName(outFile) + "\\report.txt");
                }

                // Check whether the document is valid or invalid.
                if (isValid == false)
                {
                    if (error_MasterSub != "")
                    {
                        error_Exception = manager.GetString("TranslationFailed") + "\n" + manager.GetString("WellDaisyFormat") + error_MasterSub + "\n" + error;
                    }
                    else
                    {
                        error_Exception = manager.GetString("InvalidMaster") + "\n\n" + error;
                    }
                }
                else
                {
                    if (error_MasterSub != "")
                    {
                        error_Exception = manager.GetString("TranslationFailed") + "\n\n" + manager.GetString("WellDaisyFormat") + error_MasterSub + "\n" + error;
                    }
                }
            }
            catch (UnauthorizedAccessException a)
            {
                xsd.Close();
                //dont have access permission
                error = a.Message;

                System.Windows.Forms.MessageBox.Show(error, "SaveAsDAISY", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            catch (Exception a)
            {
                xsd.Close();
                //and other things that could go wrong
                error = a.Message;

                System.Windows.Forms.MessageBox.Show(error, "SaveAsDAISY", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }