Представление данных с помощью XML и использование XPath

Автор: Пользователь скрыл имя, 19 Декабря 2011 в 13:48, лабораторная работа

Описание работы

Цель: Научиться представлять информацию в виде XML-документов, формировать их в приложениях C# и выполнять выборку интересующих данных с помощью языка XPath

Работа содержит 1 файл

моя лаба по СОУ.doc

— 215.00 Кб (Скачать)

                xw.WriteAttributeString("tema", "Референдум в Донецке");

                    xw.WriteStartElement("author")

                        xw.WriteElementString("student", "Носов Алексей Владимирович");

                        xw.WriteElementString("group", "ИУС-11зс");

                    xw.WriteEndElement();

                    if (textBox2.Text == "") textBox2.Text = "0";

                    if (textBox3.Text == "") textBox3.Text = "0";

                    string str = "/city/district[yes div took_part*100>" + Convert.ToInt16(textBox2.Text) + " and no div took_part*100>" + Convert.ToInt16(textBox3.Text) + "]/*";

                    path = nav.Compile(str);

                    XPathNodeIterator iterator = nav.Select(path);                  

                    while (iterator.MoveNext())

                    {

                        xw.WriteStartElement("district");

                            xw.WriteElementString("name", iterator.Current.Value); iterator.MoveNext();

                            double voters=Convert.ToDouble(iterator.Current.Value);

                            xw.WriteElementString("voters", voters.ToString()); iterator.MoveNext();

                            double took_part=Convert.ToDouble(iterator.Current.Value);

                            voters = Math.Round(took_part / voters * 100,2);

                            xw.WriteElementString("took_part", voters.ToString()); iterator.MoveNext();

                            double yes = Convert.ToDouble(iterator.Current.Value);

                            voters = Math.Round(yes / took_part * 100,2);

                            xw.WriteElementString("yes", voters.ToString()); iterator.MoveNext();

                            double no = Convert.ToDouble(iterator.Current.Value);

                            voters = Math.Round(no / took_part * 100,2);

                            xw.WriteElementString("no", voters.ToString());

                        xw.WriteEndElement();

                    }

                    xw.WriteStartElement("baddistrict");

                        str = "/city/district";

                        path = nav.Compile(str);

                        iterator = nav.Select(path);

                        string[] names = new string[iterator.Count];

                        double[] uch = new double[iterator.Count]; 

                        str = "/city/district/name | /city/district/voters | /city/district/took_part";

                        path = nav.Compile(str);

                        iterator = nav.Select(path);                       

                        int i = 0;

                        while (iterator.MoveNext())

                        {

                            names[i] = iterator.Current.Value; iterator.MoveNext();

                            double voters = Convert.ToDouble(iterator.Current.Value); iterator.MoveNext();                           

                            uch[i] = Math.Round(Convert.ToDouble(iterator.Current.Value)/voters*100,2);

                           

                            i++;

                        }

                        for (i = 0; i < uch.Length; i++)

                        {

                            for(int j=0;j<uch.Length-i-1;j++){

                                double tmp1; string tmp2;

                                if(uch[j]>uch[j+1]){

                                    tmp1 = uch[j + 1]; uch[j + 1] = uch[j]; uch[j] = tmp1;

                                    tmp2 = names[j + 1]; names[j + 1] = names[j]; names[j] = tmp2;

                                }

                            }

                        }

                        for (i = 0; i < 3; i++)

                        {

                            xw.WriteStartElement("district");

                                xw.WriteElementString("name", names[i]);

                                xw.WriteElementString("took_part", uch[i].ToString());

                            xw.WriteEndElement();

                        }

                    xw.WriteEndElement();

                xw.WriteEndElement();

            xw.WriteEndDocument();

            xw.Close(); 

        }

    }

} 
 

Экранные формы: 

Рисунок 1 –  Вид программы после запуска 
 

Рисунок 2 –  Вид программы после открытия файла XML 

Рисунок 3 –  Применение фильтров 

Рисунок 4 –  Открытие сохраненного файла

Информация о работе Представление данных с помощью XML и использование XPath