码迷,mamicode.com
首页 > 其他好文 > 详细

XML数据录入工具

时间:2015-05-23 23:59:04      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:

为了手机端使用XML数据库,写了一个xml录入工具。

 

XML格式如下:

<?xml version="1.0" encoding="gb2312"?>
<Trouble>
<Classification text="a" id="10000">
<Phenomena text="b" id="10100">
<Models text="c">
<Tips><![CDATA[d]]></Tips>
<Treatment><![CDATA[e]]></Treatment>
<Note><![CDATA[f]]></Note>
</Models>
</Phenomena>
</Classification>
</Trouble>

 

 技术分享

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Diagnostics;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 using System.Xml;
 12 using System.Xml.Linq;
 13 
 14 namespace XmlInput
 15 {
 16     public partial class Form1 : Form
 17     {
 18         private XDocument xdoc;
 19         private string xdocFilePath;
 20         private string currentClassification;
 21         private string currentPhenomena;
 22         private string currentModels;
 23 
 24         private string AppName = "故障信息录入";
 25         private bool 修改 = false;
 26         public Form1()
 27         {
 28             InitializeComponent();
 29             RefreshAppName();
 30         }
 31 
 32         private void RefreshAppName()
 33         {
 34             if (修改 == true)
 35             {
 36                 Text = AppName + "*";
 37             }
 38             else if (xdoc == null)
 39                 Text = AppName + " 未加载";
 40             else
 41                 Text = AppName;
 42         }
 43 
 44         private void button1_Click(object sender, EventArgs e)
 45         {
 46             OpenFileDialog o = new OpenFileDialog();
 47             if (o.ShowDialog() == DialogResult.OK)
 48             {
 49                 textBox1.Text = xdocFilePath = o.FileName;
 50                 try
 51                 {
 52                     xdoc = XDocument.Load(xdocFilePath);
 53                     RefreshAppName();
 54                     RefreshTrouble();
 55                 }
 56                 catch (Exception ex)
 57                 {
 58                     xdoc = null;
 59                     RefreshAppName();
 60                     RefreshTrouble();
 61                     MessageBox.Show(ex.Message);
 62                 }
 63             }
 64         }
 65 
 66         private void RefreshTrouble()
 67         {
 68             if (xdoc != null)
 69             {
 70                 listBox1.Items.Clear();
 71                 foreach (var p in xdoc.Element("Trouble").Elements("Classification"))
 72                 {
 73                     int i = listBox1.Items.Add(p.Attribute("text").Value);
 74                 }
 75                 RefreshClassification();
 76             }
 77             else
 78             {
 79                 currentClassification = null;
 80                 listBox1.Items.Clear();
 81                 RefreshClassification();
 82             }
 83         }
 84 
 85         private void RefreshClassification()
 86         {
 87             if (xdoc != null)
 88             {
 89                 if (listBox1.SelectedIndex != -1)
 90                 {
 91                     listBox2.Items.Clear();
 92                     string select1 = (string)listBox1.SelectedItem;
 93                     var all = xdoc.Element("Trouble").Elements("Classification").Where(o => o.Attribute("text").Value == select1).First().Elements("Phenomena");
 94                     foreach (var p in all)
 95                     {
 96                         int i = listBox2.Items.Add(p.Attribute("text").Value);
 97                     }
 98                     currentClassification = (string)listBox1.SelectedItem;
 99                 }
100                 else
101                 {
102                     currentClassification = null;
103                     listBox2.Items.Clear();
104                 }
105                 RefreshPhenomena();
106             }
107             else
108             {
109                 currentPhenomena = null;
110                 listBox2.Items.Clear();
111                 RefreshPhenomena();
112             }
113         }
114 
115         private void RefreshPhenomena()
116         {
117             if (xdoc != null)
118             {
119                 if (listBox2.SelectedIndex != -1)
120                 {
121                     listBox3.Items.Clear();
122                     string select1 = (string)listBox1.SelectedItem;
123                     string select2 = (string)listBox2.SelectedItem;
124                     var all = xdoc.Element("Trouble").Elements("Classification").Where(o => o.Attribute("text").Value == select1).First().Elements("Phenomena").Where(o => o.Attribute("text").Value == select2).First().Elements("Models");
125                     foreach (var p in all)
126                     {
127                         listBox3.Items.Add(p.Attribute("text").Value);
128                     }
129                     currentPhenomena = (string)listBox2.SelectedItem;
130                 }
131                 else
132                 {
133                     currentPhenomena = null;
134                     listBox3.Items.Clear();
135                 }
136                 RefreshModels();
137             }
138             else
139             {
140                 currentModels = null;
141                 listBox3.Items.Clear();
142                 RefreshModels();
143             }
144         }
145 
146         private void RefreshModels()
147         {
148             if (xdoc != null)
149             {
150                 if (listBox3.SelectedIndex != -1)
151                 {
152 
153                     string select1 = (string)listBox1.SelectedItem;
154                     string select2 = (string)listBox2.SelectedItem;
155                     string select3 = (string)listBox3.SelectedItem;
156                     var all = xdoc.Element("Trouble")
157                         .Elements("Classification").Where(o => o.Attribute("text").Value == select1)
158                         .First().Elements("Phenomena").Where(o => o.Attribute("text").Value == select2)
159                         .First().Elements("Models").Where(o => o.Attribute("text").Value == select3)
160                         .First();
161 
162                     textBox2.Enabled = true;
163                     textBox3.Enabled = true;
164                     textBox4.Enabled = true;
165 
166                     if (all.Elements("Tips").Count() > 0)
167                     {
168                         XCData Tips = all.Element("Tips").DescendantNodes().First() as XCData;
169                         textBox2.Text = Tips.Value;
170                     }
171                     else
172                         textBox2.Text = "";
173 
174                     if (all.Elements("Treatment").Count() > 0)
175                     {
176                         XCData Treatment = all.Element("Treatment").DescendantNodes().First() as XCData;
177                         textBox3.Text = Treatment.Value;
178                     }
179                     else
180                         textBox3.Text = "";
181 
182                     if (all.Elements("Note").Count() > 0)
183                     {
184                         XCData Note = all.Element("Note").DescendantNodes().First() as XCData;
185                         textBox4.Text = Note.Value;
186                     }
187                     else
188                         textBox4.Text = "";
189 
190                     currentModels = (string)listBox3.SelectedItem;
191                 }
192                 else
193                 {
194                     currentModels = null;
195                     textBox2.Text = "";
196                     textBox3.Text = "";
197                     textBox4.Text = "";
198                     textBox2.Enabled = false;
199                     textBox3.Enabled = false;
200                     textBox4.Enabled = false;
201 
202                 }
203             }
204             else
205             {
206                 textBox2.Text = "";
207                 textBox3.Text = "";
208                 textBox4.Text = "";
209                 textBox2.Enabled = false;
210                 textBox3.Enabled = false;
211                 textBox4.Enabled = false;
212             }
213         }
214 
215         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
216         {
217             RefreshClassification();
218         }
219 
220         private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
221         {
222             RefreshPhenomena();
223         }
224 
225         private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
226         {
227             RefreshModels();
228         }
229 
230         private void textBox2_Leave(object sender, EventArgs e)
231         {
232             Debug.WriteLine(string.Format("保存{0}:{1}:{2}", currentClassification, currentPhenomena, currentModels));
233             if (currentModels != null)
234             {
235                 var all = xdoc.Element("Trouble")
236                            .Elements("Classification").Where(o => o.Attribute("text").Value == currentClassification)
237                            .First().Elements("Phenomena").Where(o => o.Attribute("text").Value == currentPhenomena)
238                            .First().Elements("Models").Where(o => o.Attribute("text").Value == currentModels)
239                            .First();
240                 if (all.Elements("Tips").Count() == 0)
241                 {
242                     var xcd = new XCData("");
243                     var tips = new XElement("Tips");
244                     tips.Add(xcd);
245                     all.Add(tips);
246                 }
247                 (all.Element("Tips").DescendantNodes().First() as XCData).Value = textBox2.Text;
248 
249                 修改 = true;
250                 RefreshAppName();
251             }
252         }
253 
254         private void textBox3_Leave(object sender, EventArgs e)
255         {
256             Debug.WriteLine(string.Format("保存{0}:{1}:{2}", currentClassification, currentPhenomena, currentModels));
257             if (currentModels != null)
258             {
259                 var all = xdoc.Element("Trouble")
260                     .Elements("Classification").Where(o => o.Attribute("text").Value == currentClassification)
261                     .First().Elements("Phenomena").Where(o => o.Attribute("text").Value == currentPhenomena)
262                     .First().Elements("Models").Where(o => o.Attribute("text").Value == currentModels)
263                     .First();
264 
265                 if (all.Elements("Treatment").Count() == 0)
266                 {
267                     var xcd = new XCData("");
268                     var tips = new XElement("Treatment");
269                     tips.Add(xcd);
270                     all.Add(tips);
271                 }
272                 (all.Element("Treatment").DescendantNodes().First() as XCData).Value = textBox3.Text;
273 
274                 修改 = true;
275                 RefreshAppName();
276             }
277         }
278 
279         private void textBox4_Leave(object sender, EventArgs e)
280         {
281             Debug.WriteLine(string.Format("保存{0}:{1}:{2}", currentClassification, currentPhenomena, currentModels));
282             if (currentModels != null)
283             {
284                 var all = xdoc.Element("Trouble")
285                     .Elements("Classification").Where(o => o.Attribute("text").Value == currentClassification)
286                     .First().Elements("Phenomena").Where(o => o.Attribute("text").Value == currentPhenomena)
287                     .First().Elements("Models").Where(o => o.Attribute("text").Value == currentModels)
288                     .First();
289 
290                 if (all.Elements("Note").Count() == 0)
291                 {
292                     var xcd = new XCData("");
293                     var tips = new XElement("Note");
294                     tips.Add(xcd);
295                     all.Add(tips);
296                 }
297                 (all.Element("Note").DescendantNodes().First() as XCData).Value = textBox4.Text;
298 
299                 修改 = true;
300                 RefreshAppName();
301             }
302         }
303 
304         private void button2_Click(object sender, EventArgs e)
305         {
306             //SaveFileDialog o = new SaveFileDialog();
307             //if (o.ShowDialog() == DialogResult.OK)
308             //{
309             //    xdoc.Save(o.FileName);
310             //}
311             xdoc.Save(xdocFilePath);
312 
313             修改 = false;
314             RefreshAppName();
315 
316         }
317 
318         private string GetMaxIdInClassification()
319         {
320             int maxid = 0;
321             var f = xdoc.Element("Trouble").Elements("Classification");
322             if (f.Count() > 0)
323             {
324                 foreach (var p in f)
325                 {
326                     int m;
327                     if (p.Attribute("id") != null)
328                         if (int.TryParse(p.Attribute("id").Value, out m))
329                         {
330                             if (m > maxid)
331                                 maxid = m;
332                         }
333                 }
334             }
335             return maxid.ToString();
336         }
337 
338         private string GetMaxIdInPhenomena(string cid)
339         {
340             int maxid = 0;
341             var f = xdoc.Element("Trouble").Elements("Classification").Where(o => o.Attribute("text").Value == cid);
342             if (f.Count() > 0)
343             {
344                 var p = f.First().Elements("Phenomena");
345                 if (p.Count() > 0)
346                 {
347                     foreach (var i in p)
348                     {
349                         int m;
350                         if (i.Attribute("id") != null)
351                             if (int.TryParse(i.Attribute("id").Value, out m))
352                             {
353                                 if (m > maxid)
354                                     maxid = m;
355                             }
356                     }
357                 }
358                 else
359                 {
360                     int m;
361                     if (f.First().Attribute("id") != null)
362                         if (int.TryParse(f.First().Attribute("id").Value, out m))
363                         {
364                             if (m > maxid)
365                                 maxid = m;
366                         }
367                 }
368             }
369             return maxid.ToString();
370         }
371 
372 
373         private void 添加故障分类ToolStripMenuItem_Click(object sender, EventArgs e)
374         {
375             InputForm input = new InputForm("请输入故障分类");
376             if (xdoc != null)
377             {
378                 if (input.ShowDialog() == DialogResult.OK)
379                 {
380                     listBox1.Items.Add(input.InputText);
381 
382                     XElement Classification = new XElement("Classification");
383                     Classification.SetAttributeValue("text", input.InputText);
384                     Classification.SetAttributeValue("id", (int.Parse(GetMaxIdInClassification()) + 10000).ToString());
385                     xdoc.Element("Trouble").Add(Classification);
386 
387                     修改 = true;
388                     RefreshAppName();
389                 }
390             }
391         }
392 
393         private void 删除故障分类ToolStripMenuItem_Click(object sender, EventArgs e)
394         {
395             if (xdoc != null)
396             {
397                 string select = (string)listBox1.SelectedItem;
398 
399                 var f = xdoc.Element("Trouble").Elements("Classification")
400                     .Where(o => o.Attribute("text").Value == select);
401                 f.Remove();
402                 listBox1.SelectedIndex = -1;
403                 RefreshTrouble();
404 
405                 修改 = true;
406                 RefreshAppName();
407 
408             }
409         }
410 
411         private void 添加故障现象ToolStripMenuItem_Click(object sender, EventArgs e)
412         {
413             InputForm input = new InputForm("请输入故障现象");
414             if (xdoc != null && currentClassification != null)
415             {
416                 if (input.ShowDialog() == DialogResult.OK)
417                 {
418                     listBox2.Items.Add(input.InputText);
419 
420                     XElement Phenomena = new XElement("Phenomena");
421                     Phenomena.SetAttributeValue("text", input.InputText);
422                     Phenomena.SetAttributeValue("id", (int.Parse(GetMaxIdInPhenomena(currentClassification)) + 100).ToString());
423                     xdoc.Element("Trouble").Elements("Classification")
424                         .Where(o => o.Attribute("text").Value == currentClassification).First()
425                         .Add(Phenomena);
426 
427                     修改 = true;
428                     RefreshAppName();
429                 }
430             }
431         }
432 
433         private void 删除故障现象ToolStripMenuItem_Click(object sender, EventArgs e)
434         {
435             if (xdoc != null && currentClassification != null)
436             {
437                 string select = (string)listBox2.SelectedItem;
438 
439                 var f = xdoc.Element("Trouble").Elements("Classification")
440                     .Where(o => o.Attribute("text").Value == currentClassification).First()
441                     .Elements("Phenomena")
442                     .Where(o => o.Attribute("text").Value == currentPhenomena);
443                 f.Remove();
444                 listBox2.SelectedIndex = -1;
445                 RefreshClassification();
446 
447                 修改 = true;
448                 RefreshAppName();
449             }
450         }
451 
452         private void 添加车型ToolStripMenuItem_Click(object sender, EventArgs e)
453         {
454             InputForm input = new InputForm("请输入故障车型");
455             if (xdoc != null && currentPhenomena != null)
456             {
457                 if (input.ShowDialog() == DialogResult.OK)
458                 {
459                     listBox3.Items.Add(input.InputText);
460 
461                     XElement Models = new XElement("Models");
462                     Models.SetAttributeValue("text", input.InputText);
463                     //Phenomena.SetAttributeValue("id", (int.Parse(GetMaxIdInPhenomena(currentClassification)) + 1).ToString());
464                     xdoc.Element("Trouble").Elements("Classification")
465                     .Where(o => o.Attribute("text").Value == currentClassification).First()
466                     .Elements("Phenomena")
467                     .Where(o => o.Attribute("text").Value == currentPhenomena).First()
468                     .Add(Models);
469 
470                     修改 = true;
471                     RefreshAppName();
472                 }
473             }
474         }
475 
476         private void 删除车型ToolStripMenuItem_Click(object sender, EventArgs e)
477         {
478             if (xdoc != null && currentPhenomena != null)
479             {
480                 string select = (string)listBox3.SelectedItem;
481 
482                 var f = xdoc.Element("Trouble").Elements("Classification")
483                     .Where(o => o.Attribute("text").Value == currentClassification).First()
484                     .Elements("Phenomena")
485                     .Where(o => o.Attribute("text").Value == currentPhenomena).First()
486                     .Elements("Models").Where(o => o.Attribute("text").Value == currentModels);
487                 f.Remove();
488                 listBox3.SelectedIndex = -1;
489                 RefreshModels();
490 
491                 修改 = true;
492                 RefreshAppName();
493             }
494         }
495 
496         private void 重置分类IDToolStripMenuItem_Click(object sender, EventArgs e)
497         {
498             if (xdoc != null)
499             {
500                 int id = 10000;
501                 foreach (var eachClassification in xdoc.Element("Trouble").Elements("Classification"))
502                 {
503                     XAttribute a = eachClassification.Attribute("id");
504                     if (a == null)
505                         eachClassification.SetAttributeValue("id", id.ToString());
506                     else
507                         eachClassification.Attribute("id").Value = id.ToString();
508                     id += 10000;
509 
510 
511                 }
512 
513                 修改 = true;
514                 RefreshAppName();
515             }
516         }
517 
518         private void 重置现象IDToolStripMenuItem_Click(object sender, EventArgs e)
519         {
520             if (xdoc != null)
521             {
522                 var f = xdoc.Element("Trouble").Elements("Classification").Where(o => o.Attribute("text").Value == currentClassification).First();
523                 XAttribute pa = f.Attribute("id");
524                 if (pa != null)
525                 {
526                     int id = int.Parse(pa.Value) + 100;
527                     foreach (var eachPhenomena in f.Elements("Phenomena"))
528                     {
529                         XAttribute a = eachPhenomena.Attribute("id");
530                         if (a == null)
531                             eachPhenomena.SetAttributeValue("id", id.ToString());
532                         else
533                             eachPhenomena.Attribute("id").Value = id.ToString();
534                         id += 100;
535                     }
536                 }
537                 else
538                 {
539                     MessageBox.Show(string.Format("{0}没有ID值", f.Attribute("text").Value));
540                 }
541 
542                 修改 = true;
543                 RefreshAppName();
544             }
545         }
546 
547         private void 重置车型IDToolStripMenuItem_Click(object sender, EventArgs e)
548         {
549             if (xdoc != null)
550             {
551                 var f = xdoc.Element("Trouble").Elements("Classification").Where(o => o.Attribute("text").Value == currentClassification).First().Elements("Phenomena").Where(o => o.Attribute("text").Value == currentPhenomena).First();
552                 XAttribute pa = f.Attribute("id");
553                 if (pa != null)
554                 {
555                     int id = int.Parse(pa.Value) + 1;
556                     foreach (var eachModels in f.Elements("Models"))
557                     {
558                         XAttribute a = eachModels.Attribute("id");
559                         if (a == null)
560                             eachModels.SetAttributeValue("id", id.ToString());
561                         else
562                             eachModels.Attribute("id").Value = id.ToString();
563                         id += 100;
564                     }
565                 }
566                 else
567                 {
568                     MessageBox.Show(string.Format("{0}没有ID值", f.Attribute("text").Value));
569                 }
570 
571                 修改 = true;
572                 RefreshAppName();
573             }
574         }
575 
576         private void 重置所有IDToolStripMenuItem_Click(object sender, EventArgs e)
577         {
578             ResetAllID();
579         }
580 
581         private void 重置所有IDToolStripMenuItem1_Click(object sender, EventArgs e)
582         {
583             ResetAllID();
584         }
585 
586         private void 重置所有IDToolStripMenuItem2_Click(object sender, EventArgs e)
587         {
588             ResetAllID();
589         }
590 
591         private void ResetAllID()
592         {
593             if (xdoc != null)
594             {
595                 int cid = 10000;
596                 foreach (var eachClassification in xdoc.Element("Trouble").Elements("Classification"))
597                 {
598                     XAttribute xac = eachClassification.Attribute("id");
599                     if (xac == null)
600                         eachClassification.SetAttributeValue("id", cid);
601                     else
602                         eachClassification.Attribute("id").Value = cid.ToString();
603 
604                     int pid = cid + 100;
605                     foreach (var eachPhenomena in eachClassification.Elements("Phenomena"))
606                     {
607                         XAttribute xap = eachPhenomena.Attribute("id");
608                         if (xap == null)
609                             eachPhenomena.SetAttributeValue("id", pid);
610                         else
611                             eachPhenomena.Attribute("id").Value = pid.ToString();
612 
613                         int mid = pid + 1;
614                         foreach (var eachModels in eachPhenomena.Elements("Models"))
615                         {
616                             XAttribute xam = eachModels.Attribute("id");
617                             if (xam == null)
618                                 eachModels.SetAttributeValue("id", mid);
619                             else
620                                 eachModels.Attribute("id").Value = mid.ToString();
621                             mid++;
622 
623                         }
624                         pid += 100;
625 
626                     }
627                     cid += 10000;
628 
629                 }
630 
631                 修改 = true;
632                 RefreshAppName();
633 
634             }
635         }
636     }
637 }

 

XML数据录入工具

标签:

原文地址:http://www.cnblogs.com/ErrorGz/p/4525157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!