XML-Editor

Benötigte Controlls

  • Button „btn_oeffnen“
  • Button „btn_speichern“
  • dataGridView „dataGridView1“
  • openFileDialog „openFileDialog1“
  • saveFileDialog „saveFileDialog1“

Quellcode

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

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

        void Clear_all()
        {
            dataGridView1.Columns.Clear();
        }

        void ReadXML(String file)
        {
            Clear_all();
            DataSet myDataSet = new DataSet();
            myDataSet.ReadXml(file);
            dataGridView1.DataSource = myDataSet.Tables[0].DefaultView;
        }

        private void btn_oeffnen_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            ReadXML(openFileDialog1.FileName);
        }

        void SaveXML(String file)
        {
            DataTable dt = new DataTable("Adresse");

            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dt.Columns.Add(dataGridView1.Columns[i].Name, typeof(System.String));
            }

            DataRow myrow;
            int icols = dataGridView1.Columns.Count;

            foreach (DataGridViewRow drow in this.dataGridView1.Rows)
            {
                if (drow.Cells[0].Value != null)
                {
                    myrow = dt.NewRow();
                    for (int i = 0; i < icols; i++)
                    {
                        myrow[i] = drow.Cells[i].Value;
                    }
                    dt.Rows.Add(myrow);
                }
            }

            dt.WriteXml(file);
        }

        private void btn_speichern_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            SaveXML(saveFileDialog1.FileName);
        }
    }
}

Beispielinhalt einer .xml-Datei

<DocumentElement>
   <Adresse>
      <Name>Nachname1</Name>
      <Vorname>Vorname1</Vorname>
      <Postleitzahl>12345</Postleitzahl>
      <Hausnummer>22</Hausnummer>
   </Adresse>
   <Adresse>
      <Name>Nachname2</Name>
      <Vorname>Vorname2</Vorname>
      <Postleitzahl>38496</Postleitzahl>
      <Hausnummer>12</Hausnummer>
   </Adresse>
   <Adresse>
      <Name>Nachname3</Name>
      <Vorname>Vorname3</Vorname>
      <Postleitzahl>48375</Postleitzahl>
      <Hausnummer>15</Hausnummer>
   </Adresse>
   <Adresse>
      <Name>Nachname4</Name>
      <Vorname>Vorname4</Vorname>
      <Postleitzahl>38294</Postleitzahl>
      <Hausnummer>21</Hausnummer>
   </Adresse>
</DocumentElement>

Kommentar verfassen

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close