Ejemplo n.º 1
0
        public static string GetPreview(string path)
        {
            string text = "[+] " + System.IO.Path.GetFileNameWithoutExtension(path) + Environment.NewLine;

            try
            {
                using (var xml_reader = new XmlTextReader(path))
                    using (var reader = new XmlValidatingReader(xml_reader))
                    {
                        XmlSchema schema = XmlSchema.Read(new StringReader(Resources.ResourceManager.GetString("ProjectSchema")), null);
                        reader.Schemas.Add(schema);

                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element && reader.Name == "File")
                            {
                                text += " |--- " + System.IO.Path.GetFileName(reader.GetAttribute("Path")) + Environment.NewLine;
                            }
                        }
                    }
            }
            catch (Exception)
            {
                return(null);
            }

            return(text);
        }
Ejemplo n.º 2
0
    public static void Main()
    {
        //Create the validating reader.
        XmlTextReader       txtreader = new XmlTextReader("attrs.xml");
        XmlValidatingReader reader    = new XmlValidatingReader(txtreader);

        //Read the ISBN attribute.
        reader.MoveToContent();
        string isbn = reader.GetAttribute("ISBN");

        Console.WriteLine("The ISBN value: " + isbn);

        //Close the reader.
        reader.Close();
    }
Ejemplo n.º 3
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.º 4
0
        /// <summary>
        /// Открывает документ с помощью XmlValidationgReader
        /// </summary>
        private void сПомощьюXmlValidationgReaderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Файлы XML (*.xml)|*.xml";

            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            string file = dlg.FileName;

            // получаем имя файла со схемой
            dlg.Filter = "Файлы XSD (*.xsd)|*.xsd";
            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            string schema = dlg.FileName;

            XmlValidatingReader readerVal = null;

            orders.Clear();

            try
            {
                XmlTextReader reader = new XmlTextReader(file);
                reader.WhitespaceHandling = WhitespaceHandling.None;    // пропускаем пустые узлы

                readerVal = new XmlValidatingReader(reader);
                readerVal.Schemas.Add("", schema);
                readerVal.ValidationType          = ValidationType.Schema;
                readerVal.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandle);

                while (readerVal.Read())
                {
                    if (readerVal.NodeType == XmlNodeType.Element)
                    {
                        if (readerVal.Name == "Заказ")
                        {
                            Order order = new Order(readerVal.GetAttribute("Адрес"), DateTime.Parse(readerVal.GetAttribute("Дата")));

                            // получаем товары в заказе
                            while (readerVal.Read() && readerVal.Name == "Товар")
                            {
                                order.AddGood(readerVal.GetAttribute("Название"), float.Parse(readerVal.GetAttribute("Цена")));
                            }

                            orders.Add(order);
                        }
                    }
                }

                ShowOrders();
            }
            catch (XmlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка: " + ex.Message);
            }
            finally
            {
                if (readerVal != null)
                {
                    readerVal.Close();
                }
            }
        }
Ejemplo n.º 5
0
        private void LoadXML()
        {
            try
            {
                using (var xml_reader = new XmlTextReader(Path))
                    using (var reader = new XmlValidatingReader(xml_reader))
                    {
                        int fileErrorsCounter = 0;

                        XmlSchema schema = XmlSchema.Read(new StringReader(Resources.ResourceManager.GetString("ProjectSchema")), null);
                        reader.Schemas.Add(schema);

                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element && reader.Name == "Project")
                            {
                                //project.Name = reader.GetAttribute("Name");
                                //project.DefaultCodeFileExtension = reader.GetAttribute("DefaultCodeFileExtension");
                                NextDefaultFileNumber = Convert.ToInt32(reader.GetAttribute("NextDefaultFileNumber"));
                            }
                            // Files
                            else if (reader.NodeType == XmlNodeType.Element && reader.Name == "Files")
                            {
                            }
                            else if (reader.NodeType == XmlNodeType.Element && reader.Name == "File")
                            {
                                try
                                {
                                    CodeFile file = new CodeFile(reader.GetAttribute("Path"), false);
                                    AddFile(file, false);
                                }
                                catch (LoadCodeFileException ex)
                                {
                                    fileErrorsCounter++;

                                    ErrorWindow err = new ErrorWindow
                                    {
                                        ShortDesc = LocalString.Get("Error_LoadFileInProject_short") + " '" + System.IO.Path.GetFileName(ex.FileName) + "'.",
                                        FullDesc  = LocalString.Get("Error_LoadFileInProject_full"),
                                        Exception = ex
                                    };
                                    err.ShowDialog();
                                }
                            }
                            // WorkDirs
                            else if (reader.NodeType == XmlNodeType.Element && reader.Name == "WorkDirs")
                            {
                            }
                            if (reader.NodeType == XmlNodeType.Element && reader.Name == "WorkDir")
                            {
                                WorkDirs.Add(reader.GetAttribute("Path"));
                            }
                        }

                        if (fileErrorsCounter > 0)
                        {
                        }
                    }
            }
            catch (IOException ex)                  { throw new ParsingProjectException(Path, ex); }
            catch (XmlException ex)                 { throw new ParsingProjectException(Path, ex); }
            catch (XmlSchemaException ex)           { throw new ParsingProjectException(Path, ex); }
            catch (UnauthorizedAccessException ex)  { throw new ParsingProjectException(Path, ex); }
        }