XML c Documents Type Definitions

Язык C#
  1. год назад
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            XmlDocument doc = new XmlDocument();
            static bool Error = false;
            string ErrorList = null;
      
            
    
            void VallidationCallBack(object sender, ValidationEventArgs e)
            {
                Error = true;
                ErrorList += e.Message.ToString() + "\n";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.DtdProcessing = DtdProcessing.Parse;
                settings.ValidationType = ValidationType.DTD;
                settings.ValidationEventHandler += new ValidationEventHandler(VallidationCallBack);
    
                dataGridView1.Rows.Clear();
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.FileName = "";
                ofd.Multiselect = true;
                ofd.Filter = "XML|*.xml";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string souseXml = ofd.FileName;
                    XmlReader reader = XmlReader.Create(souseXml, settings);
                    try
                    {
                        while (reader.Read())
                        {
    
                        }
                    }
                    catch { MessageBox.Show("XML Error"); }
                    if (Error) { MessageBox.Show(ErrorList, "Validaton Error"); return; }
                    DataTable dt1 = new DataTable("table");
                    XmlDocument doc = new XmlDocument();
                    doc.Load(souseXml);
                    int i = 0;
                    foreach (XmlNode node in doc.DocumentElement)
                    {
                        int id = int.Parse(node["id"].InnerText);
                        string name = node["name"].InnerText;
                        int age = int.Parse(node["age"].InnerText);
                        
                        dataGridView1.Rows.Add(1);
                        dataGridView1.Rows[i].Cells[0].Value = id;
                        dataGridView1.Rows[i].Cells[1].Value = name;
                        dataGridView1.Rows[i].Cells[2].Value = age;
                        
                        i++;
                    }
                }
            }
    
          
        }
    }

    TEXT######################

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE friends [
    <!ELEMENT friends (friend)*>
    <!ELEMENT friend (id,name,age)>
    <!ATTLIST friend type CDATA #REQUIRED>
    <!ELEMENT id (#PCDATA)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT age (#PCDATA)>

    ]>
    <friends>
    <friend type = "1">
    <id> 1 </id>
    <name>Petia</name>
    <age>20</age>

    </friend>
    <friend type = "2">
    <id> 2 </id>
    <name>Vasia</name>
    <age>27</age>

    </friend>
    <friend type = "3">
    <id> 3 </id>
    <name>Lena</name>
    <age>15</age>

    </friend>
    </friends>

  2. В чем вопрос?

или зарегистрируйтесь чтобы ответить