public void reconversionToXml(string xmlFilePath) {//将XML文件逆序列化成CAN信息和信号数据库格式 //FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); // StreamReader sr = new StreamReader(fs); //string s; //while ((s = sr.ReadLine()) != null) // { // } ToXml tx = new ToXml(); object result = null; if (File.Exists(xmlFilePath)) { using (StreamReader reader = new StreamReader(xmlFilePath, System.Text.Encoding.GetEncoding("GB2312"))) { XmlSerializer xs = new XmlSerializer(tx.GetType()); result = xs.Deserialize(reader); } } tx = result as ToXml; if (!File.Exists(Conversionxmltodbc)) { string fn = Conversionxmltodbc; FileStream fs = new FileStream(Conversionxmltodbc, FileMode.Create, FileAccess.Write);//创建写入文件 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312")); foreach (CanMessageAndSignal cmas in tx.cmarr) { CanMessage canm1 = new CanMessage(); canm1 = cmas.canm; string s; s = canm1.CANmessageTAG + " " + canm1.mId + " " + canm1.messageName + canm1.mSeparator + " " + canm1.DLC + " " + canm1.mNodeName; sw.WriteLine(s); foreach (CanSignal cs in cmas.cansarr) { string s1; s1 = " " + cs.CANsignalTAG + " " + cs.signalName + " " + cs.sSeparator + " " + cs.startToEnd + " " + cs.AB + " " + cs.CD + " " + cs.unit + " " + cs.sNodeName; sw.WriteLine(s1); } sw.WriteLine(); } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 sw.Close(); fs.Close(); } else { string fn = Conversionxmltodbc; FileStream fs = new FileStream(Conversionxmltodbc, FileMode.Create, FileAccess.Write);//创建写入文件 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312")); foreach (CanMessageAndSignal cmas in tx.cmarr) { CanMessage canm1 = new CanMessage(); canm1 = cmas.canm; string s; s = canm1.CANmessageTAG + " " + canm1.mId + " " + canm1.messageName + canm1.mSeparator + " " + canm1.DLC + " " + canm1.mNodeName; sw.WriteLine(s); foreach (CanSignal cs in cmas.cansarr) { string s1; s1 = " " + cs.CANsignalTAG + " " + cs.signalName + " " + cs.sSeparator + " " + cs.startToEnd + " " + cs.AB + " " + cs.CD + " " + cs.unit + " " + cs.sNodeName; sw.WriteLine(s1); } sw.WriteLine(); } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 sw.Close(); fs.Close(); } }
public void conversionToXml(string fileName) { //将dbc文件转换为XML文件 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312")); ToXml tx = new ToXml(); tx.cmarr = new List <CanMessageAndSignal>(); CanMessage tm; CanMessageAndSignal cms = new CanMessageAndSignal(); string s; List <CanSignal> cs = new List <CanSignal>(); //信号为ts,与之相对应 int[] signalnum = new int [100]; // 每条CAN信息中的CAN Signal的数目 不能够超过99,这个数组熊1开始 int snum = 0; // 每条CAN信息中的CAN Signal的数目 int cnt = 0; while ((s = sr.ReadLine()) != null) { string[] arr = s.Split(' '); if (string.IsNullOrWhiteSpace(s)) { cs = new List <CanSignal>(); continue; } else if (arr.Length == 5) //CAN message { // cs = new List<CanSignal>(); cms = new CanMessageAndSignal(); tm = new CanMessage(); cs = new List <CanSignal>(); cms.canm = tm; cms.cansarr = cs; tx.cmarr.Add(cms); tm.CANmessageTAG = arr[0]; tm.mId = Convert.ToUInt32(arr[1]); int n;//表示“:”在数组中的位置; n = arr[2].IndexOf(':'); tm.messageName = arr[2].Substring(0, n); tm.mSeparator = ":"; tm.DLC = Convert.ToInt32(arr[3]); tm.mNodeName = arr[4]; signalnum[cnt] = snum; // 这个数组从1开始 cnt++; snum = 0; } else { string[] arr1 = s.Trim().Split(' '); CanSignal ts = new CanSignal(); ts.CANsignalTAG = arr1[0]; //Console.Write(arr1[0]); //Console.Write(arr1[1]); ts.signalName = arr1[1]; //Console.Write(arr1[2]); ts.sSeparator = arr1[2]; ts.startToEnd = arr1[3]; ts.AB = arr1[4]; ts.CD = arr1[5]; ts.unit = arr1[6]; ts.sNodeName = arr1[8]; cs.Add(ts); snum++; } } signalnum[cnt] = snum; // 这个数组从1开始 if (!File.Exists(ConversionPath)) { string fn = ConversionPath; FileStream fs1 = new FileStream(ConversionPath, FileMode.Create, FileAccess.Write);//创建写入文件 //StreamWriter sw1 = new StreamWriter(fs1); if (tx != null && !string.IsNullOrEmpty(fn)) { using (StreamWriter sw1 = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312"))) { XmlSerializer xs = string.IsNullOrEmpty(null) ? new XmlSerializer(tx.GetType()) : new XmlSerializer(tx.GetType(), new XmlRootAttribute(null)); xs.Serialize(sw1, tx); sw1.Close(); } } //sw.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 fs1.Close(); } else { string fn = ConversionPath; FileStream fs1 = new FileStream(ConversionPath, FileMode.Create, FileAccess.Write);//创建写入文件 //StreamWriter sw1 = new StreamWriter(fs1); if (tx != null && !string.IsNullOrEmpty(fn)) { using (StreamWriter sw1 = new StreamWriter(fs1, System.Text.Encoding.GetEncoding("GB2312"))) { XmlSerializer xs = string.IsNullOrEmpty(null) ? new XmlSerializer(tx.GetType()) : new XmlSerializer(tx.GetType(), new XmlRootAttribute(null)); xs.Serialize(sw1, tx); sw1.Close(); } } //sr.WriteLine(this.textBox3.Text.Trim() + "+" + this.textBox4.Text);//开始写入值 fs1.Close(); } //int a = signalnum[1]; }