Запись в XML - C# (185286)
Формулировка задачи:
Здравствуйте! Данный код записывает данные в следующем формате
как сделать чтобы каждый шаг цикла данные поля добавлял еще и в <c>
тобишь типа
public void Export()
{
XmlTextWriter textWritter = new XmlTextWriter("ListCountry.xml", null);
textWritter.WriteStartDocument();
textWritter.WriteStartElement("ListCountry");
textWritter.WriteEndElement();
textWritter.Close();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("ListCountry.xml");
DataBase.SqlSelect("SELECT * FROM `country`");
if (DataBase.SqlResult.Length > 0)
{
for (int i = 0; i < DataBase.SqlResult.Length; i++)
{
XmlElement subRoot = xmlDoc.CreateElement("Name");
subRoot.InnerText = DataBase.SqlResult[i].ItemArray[0] + "";
xmlDoc.DocumentElement.AppendChild(subRoot);
XmlElement appendedElementCount = xmlDoc.CreateElement("capital");
appendedElementCount.InnerText = DataBase.SqlResult[i].ItemArray[2] + "";
xmlDoc.DocumentElement.AppendChild(appendedElementCount);
XmlElement mainland = xmlDoc.CreateElement("mainland");
mainland.InnerText = DataBase.SqlResult[i].ItemArray[1] + "";
xmlDoc.DocumentElement.AppendChild(mainland);
XmlElement people = xmlDoc.CreateElement("people");
people.InnerText = DataBase.SqlResult[i].ItemArray[3] + "";
xmlDoc.DocumentElement.AppendChild(people);
XmlElement area = xmlDoc.CreateElement("area");
area.InnerText = DataBase.SqlResult[i].ItemArray[4] + "";
xmlDoc.DocumentElement.AppendChild(area);
}
}
else MessageBox.Show("БД пуста! Експорт зробити не можливо...", "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);
xmlDoc.Save("ListCountry.xml");
}<?xml version="1.0"?> <ListCountry> <Name>uk</Name> <capital>Київ</capital> <mainland>Європа</mainland> <people>125</people> <area>313113</area> <Name>eng</Name> <capital> </capital> <mainland> </mainland> <people>0</people> <area>0</area> </ListCountry>
<c> <Name>eng</Name> <capital> </capital> <mainland> </mainland> <people>0</people> <area>0</area> </c>
Решение задачи: «Запись в XML»
textual
Листинг программы
using System;
using System.Xml;
namespace thread1737352
{
class Program
{
static void Main(string[] args)
{
XmlTextWriter textWritter = new XmlTextWriter("ListCountry.xml", null);
textWritter.WriteStartDocument();
textWritter.WriteStartElement("ListCountry");
textWritter.WriteEndElement();
textWritter.Close();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("ListCountry.xml");
string[] info = init();
foreach (string line in info)
{
string[] split = line.Split(new char[] { ' ' });
XmlElement cElement = xmlDoc.CreateElement("C");
addInfoAboutnXml("Name", split[0], xmlDoc.DocumentElement, xmlDoc);
addInfoAboutnXml("Name", split[0], cElement, xmlDoc);
addInfoAboutnXml("Capital", split[1], xmlDoc.DocumentElement, xmlDoc);
addInfoAboutnXml("Capital", split[1], cElement, xmlDoc);
addInfoAboutnXml("Mainland", split[2], xmlDoc.DocumentElement, xmlDoc);
addInfoAboutnXml("Mainland", split[2], cElement, xmlDoc);
addInfoAboutnXml("People", split[3], xmlDoc.DocumentElement, xmlDoc);
addInfoAboutnXml("People", split[3], cElement, xmlDoc);
xmlDoc.DocumentElement.AppendChild(cElement);
}
xmlDoc.Save("ListCountry.xml");
}
/// <summary>
/// Добавление дочерних элементов
/// </summary>
/// <param name="_tagName">Название тега</param>
/// <param name="_text">Текс который нужно разместить</param>
/// <param name="_rootElemetn">Корневой тег, в который нужно поместить</param>
/// <param name="_xmlDoc">Документ</param>
static void addInfoAboutnXml(string _tagName, string _text, XmlElement _rootElemetn, XmlDocument _xmlDoc)
{
XmlElement createElement = _xmlDoc.CreateElement(_tagName);
createElement.InnerText = _text;
_rootElemetn.AppendChild(createElement);
}
static string[] init()
{
string[] ret = { "Name1 capital1 mainland1 people1 area1", "Name2 capital2 mainland2 people2 area2", "Name3 capital3 mainland3 people3 area3" };
return ret;
}
}
}