Программное создание XML-файла - C#
Формулировка задачи:
Здравствуйте! Необходимо программно создать файл следующего вида:
Как программно правильно его создать? Я делал так:
в итоге получил:
С XML работаю первый раз. Хотелось бы разобраться. Заранее благодарен!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace писалка_XML
{
public partial class Form1 : Form
{
static string pathToXml = @"C:\Users\1\Desktop\1.xml";
XmlTextWriter textWritter = new XmlTextWriter(pathToXml, Encoding.UTF8);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textWritter.WriteStartDocument();
textWritter.WriteStartElement("test");
textWritter.WriteEndElement();
textWritter.Close();
XmlDocument document = new XmlDocument();
document.Load(pathToXml);
XmlNode elementen = document.CreateElement("Theme");
document.DocumentElement.AppendChild(elementen); // указываем родителя
elementen.InnerText = "Dear";
XmlNode element = document.CreateElement("Question");
document.DocumentElement.AppendChild(element); // указываем родителя
XmlAttribute attribute = document.CreateAttribute("number"); // создаём атрибут
attribute.Value = "1"; // устанавливаем значение атрибута
element.Attributes.Append(attribute); // добавляем атрибут
XmlNode subelement = document.CreateElement("question1");
document.DocumentElement.AppendChild(element); // указываем родителя
XmlAttribute attributen = document.CreateAttribute("text"); // создаём атрибут
attributen.Value = "Вопрос1"; // устанавливаем значение атрибута
subelement.Attributes.Append(attributen); // добавляем атрибут
XmlAttribute attributen2 = document.CreateAttribute("true"); // создаём атрибут
attributen2.Value = "question"; // устанавливаем значение атрибута
subelement.Attributes.Append(attributen2); // добавляем атрибут
XmlNode subElement22 = document.CreateElement("Answer");
document.DocumentElement.AppendChild(subelement); // указываем родителя
subElement22.InnerText = "33";
element.AppendChild(subelement);
XmlNode subElement2 = document.CreateElement("question2");
subElement2.InnerText = "1111";
element.AppendChild(subElement2);
XmlNode subElement3 = document.CreateElement("question3");
subElement3.InnerText = "3333";
element.AppendChild(subElement3);
document.Save(pathToXml);
}
}
}Решение задачи: «Программное создание XML-файла»
textual
Листинг программы
XDocument xdoc = new XDocument(
new XDeclaration("1.0", "Windows-1251", ""),
new XElement("test",
new XElement("Theme", "Тема"),
new XElement("questionNumbers", new XAttribute("numbers", 3),
new XElement("question1", new XAttribute("text", "Вопрос1"), new XAttribute("true", "ответ3"),
new XElement("answers", "ответ1|ответ2|ответ3|ответ4")),
new XElement("question2", new XAttribute("text", "2+2:"), new XAttribute("true", "ответ3"),
new XElement("answers", "ответ1|ответ2|ответ3|ответ4")),
new XElement("question3", new XAttribute("text", "Вопрос2"), new XAttribute("true", "ответ3"),
new XElement("answers", "ответ1|ответ2|ответ3|ответ4"))
)
)
);
xdoc.Save("test.xml");