読者です 読者をやめる 読者になる 読者になる

bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

FormでXML入力、読込と保存

フォームで入力した情報をXMLで保存、保存したXMLを読み込みできるようにした。


<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--パーティクルデータ-->
<particle>
  <header>
    <name type="string">abc</name>
  </header>
  <emitter>
    <rotX type="Int16">1</rotX>
    <rotY type="Int16">2</rotY>
    <rotZ type="Int16">3</rotZ>
  </emitter>
  <particle>
    <num type="UInt16">4</num>
    <interval type="UInt16">5</interval>
    <life type="UInt16">6</life>
  </particle>
</particle>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;

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

        private void button_save_Click(object sender, EventArgs e)
        {
            // 情報の取得
            string basename = textBox_name.Text;
            string filename = basename + ".xml";
            Int16 emitter_rotX = (Int16)numUpDown_emitter_rotX.Value;
            Int16 emitter_rotY = (Int16)numUpDown_emitter_rotY.Value;
            Int16 emitter_rotZ = (Int16)numUpDown_emitter_rotZ.Value;
            UInt16 emit_num = (UInt16)numUpDown_emit_num.Value;
            UInt16 emit_interval = (UInt16)numUpDown_emit_interval.Value;
            UInt16 life = (UInt16)numUpDown_life.Value;

            // 保存する
            saveFileDialog.FileName = filename;
            saveFileDialog.InitialDirectory = @"D:\text\xml";
            if(saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                var particle_xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
                particle_xml.Add(new XComment("パーティクルデータ"));
                particle_xml.Add(new XElement("particle")); // Root

                // header
                XElement elem = new XElement("header");
                    XElement sub_elem = new XElement("name", basename);
                    sub_elem.Add(new XAttribute("type", "string"));
                elem.Add(sub_elem);
                particle_xml.Root.Add(elem);

                // emitter
                elem = new XElement("emitter");
                    sub_elem = new XElement("rotX", emitter_rotX);
                    sub_elem.Add(new XAttribute("type", "Int16"));
                    elem.Add(sub_elem);

                    sub_elem = new XElement("rotY", emitter_rotY);
                    sub_elem.Add(new XAttribute("type", "Int16"));
                    elem.Add(sub_elem);

                    sub_elem = new XElement("rotZ", emitter_rotZ);
                    sub_elem.Add(new XAttribute("type", "Int16"));
                    elem.Add(sub_elem);
                particle_xml.Root.Add(elem);

                // particle
                elem = new XElement("particle");
                    sub_elem = new XElement("num", emit_num);
                    sub_elem.Add(new XAttribute("type", "UInt16"));
                    elem.Add(sub_elem);

                    sub_elem = new XElement("interval", emit_interval);
                    sub_elem.Add(new XAttribute("type", "UInt16"));
                    elem.Add(sub_elem);

                    sub_elem = new XElement("life", life);
                    sub_elem.Add(new XAttribute("type", "UInt16"));
                    elem.Add(sub_elem);
                particle_xml.Root.Add(elem);

                string save_path = saveFileDialog.FileName;
                particle_xml.Save(save_path);
            }
        }

        private void button_load_Click(object sender, EventArgs e)
        {
            // 読込
            openFileDialog.InitialDirectory = @"D:\text\xml";
            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog.FileName;
                var load_xml = XDocument.Load(@filename);

                // header
                var query = from x in load_xml.Descendants("header") select x;
                textBox_name.Text = query.Elements("name").ElementAt(0).Value;

                // emitter
                query = from x in load_xml.Descendants("emitter") select x;
                numUpDown_emitter_rotX.Value = Int16.Parse(query.Elements("rotX").ElementAt(0).Value);
                numUpDown_emitter_rotY.Value = Int16.Parse(query.Elements("rotY").ElementAt(0).Value);
                numUpDown_emitter_rotZ.Value = Int16.Parse(query.Elements("rotZ").ElementAt(0).Value);

                // particle
                query = from x in load_xml.Descendants("particle") select x;
                numUpDown_emit_num.Value = UInt16.Parse(query.Elements("num").ElementAt(0).Value);
                numUpDown_emit_interval.Value = UInt16.Parse(query.Elements("interval").ElementAt(0).Value);
                numUpDown_life.Value = UInt16.Parse(query.Elements("life").ElementAt(0).Value);
            }
        }
    }
}

広告を非表示にする