V
V
vladimirchelyabinskiy2016-04-11 07:55:29
C++ / C#
vladimirchelyabinskiy, 2016-04-11 07:55:29

C# how to add multiple elements with parameters to the generated XML document?

The task is the following, when you click the "Create" button, an XML document is created with 1 element ("Text" and its attributes), when you click the "More" button, the Textbox fields are cleared .....
....
and here I need to add to the already created document more "Text" elements with other attributes, for example, 6 more pieces.
Tell me how to do this? Preferably a finished example!

namespace XMLs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) // Кнопка создать
        {
            // Создаём документ XML
            XDocument doc = new XDocument(new XDeclaration("1.0", "windows-1251", null));
            
            // Создаем элемент "File"
            XElement el_file = new XElement("File");

            // Добавляем необходимые атрибуты
            el_file.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));
            el_file.Add(new XAttribute("id", 1));
            el_file.Add(new XAttribute("version", "2.0"));
            doc.Add(el_file);

            // Создаем элемент "Document"
            XElement el_doc = new XElement("Document");

            // Добавляем необходимые атрибуты
            el_doc.Add(new XAttribute("DocId", 11512));
            el_doc.Add(new XAttribute("Date", "12.07.15"));


            // создаём элемент "Text"
            XElement el_svnp = new XElement("Text");

            // Добавляем необходимые атрибуты
            el_svnp.Add(new XAttribute("a", textBox1.Text));
            el_svnp.Add(new XAttribute("b", textBox2.Text));
            el_doc.Add(el_svnp);

            doc.Root.Add(el_doc);
            doc.Save("_s.temp");
        }

        private void button2_Click(object sender, EventArgs e) // Кнопка "Еще"
        {
            textBox1.Text = null;
            textBox2.Text = null;
        }

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Iron Bug, 2016-04-11
@vladimirchelyabinskiy

You need to take xdocument into a class variable, and in the handler check if the document is null, then create it, if not null, then find the Text element and add new nodes after it.

public partial class Form1 : Form
{
    private readonly XDocument _xml;
    public Form1()
    {
        InitializeComponent();
        _xml = CreateNewXml();
    }

    private void button1_Click(object sender, EventArgs e) 
    {        
        var docNode = _xml.Root.Element("Document");
        var lastTextNode = docNode.Elements("Text").LastOrDefault();
        var textNode = CreateNewTextNode();
        if (lastTextNode != null)
            lastTextNode.AddAfterSelf(textNode);
        else
            docNode.Add(textNode);
        
        _xml.Save("_s.temp");
    }

    private XDocument CreateNewXml()
    {
        var doc = new XDocument(new XDeclaration("1.0", "windows-1251", null));
        var fileNode = new XElement("File");
        fileNode.Add(new XAttribute(XNamespace.Xmlns + "xsi", 
            "http://www.w3.org/2001/XMLSchema-instance"));
        fileNode.Add(new XAttribute("id", 1),
            new XAttribute("version", "2.0"));            
        XElement docNode = new XElement("Document");
        docNode.Add(new XAttribute("DocId", 11512),
            new XAttribute("Date", "12.07.15"));

        fileNode.Add(docNode);
        doc.Add(fileNode);

        return doc;
    }

    private XElement CreateNewTextNode()
    {
        XElement textNode = new XElement("Text");
        textNode.Add(
            new XAttribute("a", textBox1.Text),
            new XAttribute("b", textBox2.Text));            
        return textNode;
    }

    private void button2_Click(object sender, EventArgs e) 
    {
        textBox1.Text = null;
        textBox2.Text = null;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question