Заполнение XML - C#
Формулировка задачи:
Здравствуйте! Есть xml-файл
и есть простенькая бд, скрин во вложении.
Как мне получить XML-файл типа
Используя следующий код
получаю такой файл
Где не прав, подскажите. Уже совсем запутался!
<?xml version="1.0" encoding="utf-8" ?> - <bookstore> - <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> - <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2"> <title>The Confidence Man</title> - <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>11.99</price> </book> - <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6"> <title>The Gorgias</title> - <author> <first-name>Plato</first-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>9.99</price> </book> </bookstore>
<?xml version="1.0" encoding="utf-8" ?> - <bookstore> - <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> - <author> - <first-name> Benjamin <rotor channel="1tv" day="Wednesday" time="13:00" /> </first-name> <last-name>Franklin</last-name> </author> - <author> - <first-name> Benjamin <rotor channel="1tv" day="Monday" time="15:00" /> </first-name> <last-name>Franklin</last-name> </author> - <author> - <first-name> Benjamin <rotor channel="1tv" day="everyday" time="01:00" /> </first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> - <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2"> <title>The Confidence Man</title> - <author> <first-name> Herman <rotor channel="ntv" day="Monday" time="17:00" /> </first-name> <last-name>Melville</last-name> </author> - <author> <first-name> Benjamin <rotor channel="ntv" day="everyday" time="21:00" /> </first-name> <last-name>Franklin</last-name> </author> - <author> <first-name> Benjamin <rotor channel="ntv" day="Monday" time="22:00" /> </first-name> <last-name>Franklin</last-name> </author> <price>11.99</price> </book> - <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6"> <title>The Gorgias</title> - <author> <first-name> Plato <rotor channel="tnt" day="Friday" time="14:00" /> </first-name> </author> - <author> <first-name> Benjamin <rotor channel="tnt" day="everyday" time="20:00" /> </first-name> <last-name>Franklin</last-name> </author> - <author> <first-name> Benjamin <rotor channel="tnt" day="Monday" time="23:00" /> </first-name> <last-name>Franklin</last-name> </author> <price>9.99</price> </book> </bookstore>
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("contosoBooks.xml"); string [] tb = {textBox1.Text, textBox2.Text, textBox3.Text}; XmlNodeList userNodes = xmlDoc.SelectNodes("//bookstore/book/author/first-name"); //Соединяюсь с бд, заполняю DataRow int i = 0; int k = 0; while (i < tb.Length) { foreach (XmlNode userNode in userNodes) { if (k < userNodes.Count) { DataRow[] result = results.Select("Channel = '" + tb[i] + "'"); foreach (DataRow row in result) { XmlElement elem = xmlDoc.CreateElement("rotor"); elem.SetAttribute("channel", row["Channel"].ToString()); elem.SetAttribute("day", row["day"].ToString()); elem.SetAttribute("time", row["time"].ToString()); userNode.AppendChild(elem); k++; } } i++; } } xmlDoc.Save("test-doc.xml");
<?xml version="1.0" encoding="utf-8" ?> - <bookstore> - <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> - <author> - <first-name> Benjamin <rotor channel="1tv" day="Wednesday" time="13:00" /> <rotor channel="1tv" day="Monday" time="15:00" /> <rotor channel="1tv" day="everyday" time="01:00" /> </first-name> <last-name>Franklin</last-name> </author> - <author> - <first-name> Benjamin <rotor channel="ntv" day="Monday" time="17:00" /> <rotor channel="ntv" day="everyday" time="21:00" /> <rotor channel="ntv" day="Monday" time="22:00" /> </first-name> <last-name>Franklin</last-name> </author> - <author> - <first-name> Benjamin <rotor channel="tnt" day="Friday" time="14:00" /> <rotor channel="tnt" day="everyday" time="20:00" /> <rotor channel="tnt" day="Monday" time="23:00" /> </first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> - <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2"> <title>The Confidence Man</title> - <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>11.99</price> </book> - <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6"> <title>The Gorgias</title> - <author> <first-name>Plato</first-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> - <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>9.99</price> </book> </bookstore>
Решение задачи: «Заполнение XML»
textual
Листинг программы
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.OleDb; namespace ConsoleApplication5 { class DataBaseOperator { OleDbDataAdapter dAdapter; public DataBaseOperator() { OleDbConnection connection=new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;"+ @"Data Source=C:\DataBase.accdb;Persist Security Info=False"); OleDbCommand cmd = new OleDbCommand("SELECT * FROM DataTable", connection); dAdapter = new OleDbDataAdapter(); dAdapter.SelectCommand = cmd; } public DataTable GetDataTable() { DataTable dt = new DataTable(); dAdapter.Fill(dt); return dt; } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д