非UI线程处理XML格式转换

  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;
 10 using System.Threading.Tasks;
 11 using System.Windows.Forms;
 12 using System.Xml;
 13 using System.Xml.Linq;
 14 
 15 namespace xmlConvnet
 16 {
 17     public partial class Form1 : Form
 18     {
 19         string inputFilePath, outputFilePath;
 20         public Form1()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         private void button1_Click(object sender, EventArgs e)
 26         {
 27             OpenFileDialog o = new OpenFileDialog();
 28             if (o.ShowDialog() == DialogResult.OK)
 29             {
 30                 inputFilePath = o.FileName;
 31                 textBox1.Text = inputFilePath;
 32             }
 33         }
 34 
 35         private void button2_Click(object sender, EventArgs e)
 36         {
 37             if (inputFilePath != null && outputFilePath != null)
 38             {
 39                 Thread t = new Thread(new ThreadStart(() =>
 40                 {
 41                     XDocument inputXmlFile = XDocument.Load(inputFilePath);
 42                     XElement inputTrouble = inputXmlFile.Element("Trouble");
 43                     XmlDocument outputXmlFile = new XmlDocument();
 44                     outputXmlFile.AppendChild(outputXmlFile.CreateXmlDeclaration("1.0", "GB2312", null));
 45                     XmlElement outputTrouble = outputXmlFile.CreateElement("Trouble");
 46                     string msg = "开始";
 47                     ShowText(msg);
 48 
 49                     foreach (var inputClassification in inputTrouble.Elements("Classification"))
 50                     {
 51                         ShowText(inputClassification.Attribute("text").Value);
 52                         XmlElement outputClassification = outputXmlFile.CreateElement("Classification");
 53                         outputClassification.SetAttribute("text", inputClassification.Attribute("text").Value);
 54                         outputClassification.SetAttribute("id", inputClassification.Attribute("id").Value);
 55 
 56 
 57                         foreach (var inputPhenomena in inputClassification.Elements("Phenomena"))
 58                         {
 59                             ShowText("    "+inputPhenomena.Attribute("text").Value);
 60                             XmlElement outputPhenomena = outputXmlFile.CreateElement("Phenomena");
 61                             outputPhenomena.SetAttribute("text", inputPhenomena.Attribute("text").Value);
 62                             outputPhenomena.SetAttribute("id", inputPhenomena.Attribute("id").Value);
 63 
 64 
 65                             List<Phenomena> listP = new List<Phenomena>();
 66 
 67                             #region 处理Phenomena(故障现象)段
 68                             {
 69                                 IEnumerable<XNode> n = inputPhenomena.Element("Treatment").DescendantNodes();
 70                                 if (n.Count() > 0)
 71                                 {
 72                                     XCData xTreatment = n.First() as XCData;
 73                                     string strTreatment = xTreatment.Value;
 74                                     var lines = strTreatment.Split('\n').ToList();
 75 
 76                                     Phenomena p = null;
 77 
 78                                     for (int i = 0; i < lines.Count; i++)
 79                                     {
 80                                         string line = lines[i];
 81                                         line = line.Replace("?", " ").Replace('\t', ' ').Trim();
 82                                         if (line == "")
 83                                             continue;
 84                                         if (line.Contains("B1型电客车(3改6)"))
 85                                         {
 86                                             if (p != null)
 87                                                 listP.Add(p);
 88                                             p = new Phenomena();
 89                                             p.Modes = "B1型车";
 90                                         }
 91                                         else if (line.Contains("(03087088车外列车)"))
 92                                         {
 93                                             if (p != null)
 94                                                 listP.Add(p);
 95                                             p = new Phenomena();
 96                                             p.Modes = "B2、B4型车(除8788车)";
 97                                         }
 98                                         else if (line.Contains("(03087088列车)"))
 99                                         {
100                                             if (p != null)
101                                                 listP.Add(p);
102                                             p = new Phenomena();
103                                             p.Modes = "B2、B4型车(仅8788车)";
104                                         }
105                                         else if (line.Contains("B2、B4型电客车"))
106                                         {
107                                             if (p != null)
108                                                 listP.Add(p);
109                                             p = new Phenomena();
110                                             p.Modes = "B2、B4型车";
111                                         }
112                                         else if (line.Contains("B4型电客车(国产牵引列车)"))
113                                         {
114                                             if (p != null)
115                                                 listP.Add(p);
116                                             p = new Phenomena();
117                                             p.Modes = "国产牵引列车";
118                                         }
119                                         else
120                                         {
121                                             if (p != null)
122                                             {
123                                                 p.Tips = "";
124                                                 p.Treatment += line + "\r\n";
125 
126                                             }
127                                         }
128                                     }
129 
130                                     listP.Add(p);
131 
132 
133                                 }
134                             }
135 
136                             {
137                                 IEnumerable<XNode> n = inputPhenomena.Element("Note").DescendantNodes();
138                                 if (n.Count() > 0)
139                                 {
140                                     XCData xNote = n.First() as XCData;
141                                     string strNote = xNote.Value;
142                                     var lines = strNote.Split('\n').ToList();
143 
144                                     Phenomena p = null;
145                                     for (int i = 0; i < lines.Count; i++)
146                                     {
147                                         string line = lines[i];
148                                         line = line.Replace("?", " ").Replace('\t', ' ').Trim();
149                                         if (line == "")
150                                             continue;
151                                         if (line.Contains("B1型电客车(3改6)"))
152                                         {
153                                             foreach (var ep in listP)
154                                             {
155                                                 if (ep.Modes == "B1型车")
156                                                     p = ep;
157                                             }
158                                         }
159                                         else if (line.Contains("(03087088车外列车)"))
160                                         {
161                                             foreach (var ep in listP)
162                                             {
163                                                 if (ep.Modes == "B2、B4型车(除8788车)")
164                                                     p = ep;
165                                             }
166                                         }
167                                         else if (line.Contains("(03087088列车)"))
168                                         {
169                                             foreach (var ep in listP)
170                                             {
171                                                 if (ep.Modes == "B2、B4型车(仅8788车)")
172                                                     p = ep;
173                                             }
174                                         }
175                                         else if (line.Contains("B2、B4型电客车"))
176                                         {
177                                             foreach (var ep in listP)
178                                             {
179                                                 if (ep.Modes == "B2、B4型车")
180                                                     p = ep;
181                                             }
182                                         }
183                                         else if (line.Contains("B4型电客车(国产牵引列车)"))
184                                         {
185                                             foreach (var ep in listP)
186                                             {
187                                                 if (ep.Modes == "国产牵引列车")
188                                                     p = ep;
189                                             }
190                                         }
191                                         else
192                                         {
193                                             if (p != null)
194                                             {
195                                                 p.Tips = "";
196                                                 p.Note += line + "\r\n";
197 
198                                             }
199                                             else
200                                             {
201                                                 Debug.WriteLine(string.Format("--------没有类型的说明:{0}", line));
202                                             }
203                                         }
204                                     }
205 
206                                 }
207                             }
208                             #endregion
209 
210                             //
211                             StringBuilder sb = new StringBuilder();
212                             foreach (var line in listP)
213                             {
214                                 sb.Append(line.Modes).Append("\t");
215 
216                             }
217                             Debug.WriteLine(sb.ToString());
218 
219                             foreach (var p in listP)
220                             {
221                                 ShowText("        " + p.Modes);
222                                 XmlElement Models = outputXmlFile.CreateElement("Models");
223                                 Models.SetAttribute("text", p.Modes);
224 
225                                 XmlElement Tips = outputXmlFile.CreateElement("Tips");
226                                 Tips.AppendChild(outputXmlFile.CreateCDataSection(""));
227                                 Models.AppendChild(Tips);
228 
229                                 XmlElement Treatment = outputXmlFile.CreateElement("Treatment");
230                                 Treatment.AppendChild(outputXmlFile.CreateCDataSection(p.Treatment));
231                                 Models.AppendChild(Treatment);
232 
233                                 XmlElement Note = outputXmlFile.CreateElement("Note");
234                                 Note.AppendChild(outputXmlFile.CreateCDataSection(p.Note));
235                                 Models.AppendChild(Note);
236 
237                                 outputPhenomena.AppendChild(Models);
238                             }
239                             outputClassification.AppendChild(outputPhenomena);
240                         }
241                         outputTrouble.AppendChild(outputClassification);
242 
243                     }
244 
245                     outputXmlFile.AppendChild(outputTrouble);
246                     outputXmlFile.Save(outputFilePath);
247                     ShowText("结束");
248                 }));
249                 t.Start();
250             }
251         }
252 
253         private void ShowText(string msg)
254         {
255             textBox2.Invoke((Action)delegate
256             {
257                 textBox2.Text += msg + "\r\n";
258             });
259         }
260 
261         private void button3_Click(object sender, EventArgs e)
262         {
263             SaveFileDialog o = new SaveFileDialog();
264             if (o.ShowDialog() == DialogResult.OK)
265             {
266                 outputFilePath = o.FileName;
267                 textBox3.Text = outputFilePath;
268             }
269         }
270     }
271 }

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace xmlConvnet
 8 {
 9     public class Phenomena
10     {
11         public string Modes;
12         public string Tips;
13         public string Treatment;
14         public string Note;
15     }
16 }

 

posted on 2015-05-23 23:32  ErrorGz  阅读(134)  评论(0)    收藏  举报

导航