XML指定格式数据写入
private static void GenerateXMLFile(string xMLname,string SCC_Board, string SOC_Board,string ECU,string QNX,string android_image,string SCC_BL, string SCC_APP,string Ethernet,string efuse,string SCC_BT) { try { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; //settings.Encoding = new UTF8Encoding(false); settings.NewLineChars = Environment.NewLine; using (XmlWriter xmlWriter = XmlWriter.Create("D:\\个人资料\\训练\\C#\\XML_Read and Write\\config\\" + xMLname + ".xml", settings)) { xmlWriter.WriteStartDocument(true); xmlWriter.WriteStartElement("LVData"); xmlWriter.WriteAttributeString("test","xmlns"+"http://www.ni.com/LVData"); xmlWriter.WriteElementString("Version", "20"); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name","SCC Board ID"); xmlWriter.WriteElementString("Val", SCC_Board); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "SOC Board ID"); xmlWriter.WriteElementString("Val", SOC_Board); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "ECU Core PN"); xmlWriter.WriteElementString("Val", ECU); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "QNX Version"); xmlWriter.WriteElementString("Val", QNX); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "Android Image Version"); xmlWriter.WriteElementString("Val", android_image); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "SCC BL Version"); xmlWriter.WriteElementString("Val", SCC_BL); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "SCC APP Version"); xmlWriter.WriteElementString("Val", SCC_APP); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "Ethernet Switch Version"); xmlWriter.WriteElementString("Val", Ethernet); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "Efuse Status"); xmlWriter.WriteElementString("Val", efuse); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("String"); xmlWriter.WriteElementString("Name", "SCC Boot Version"); xmlWriter.WriteElementString("Val", SCC_BT); xmlWriter.WriteEndElement(); xmlWriter.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
1、XML文件数据写入,方法可以参考上面的程序,写入效果如下:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <LVData test="xmlnshttp://www.ni.com/LVData"> <Version>20</Version> <String> <Name>SCC Board ID</Name> <Val>0701</Val> </String> <String> <Name>SOC Board ID</Name> <Val>25.6.0.15</Val> </String> <String> <Name>ECU Core PN</Name> <Val>8895108191202042</Val> </String> <String> <Name>QNX Version</Name> <Val>rb-ecarx-dhu_hqx121c1-pcs01_ota1_release_2022.42.7</Val> </String> <String> <Name>Android Image Version</Name> <Val>R2R0222420224202</Val> </String> <String> <Name>SCC BL Version</Name> <Val>BL_OTA1_2022.42.7</Val> </String> <String> <Name>SCC APP Version</Name> <Val>FX11_OTA1_2022.42.7</Val> </String> <String> <Name>Ethernet Switch Version</Name> <Val>v2.08.1002 (2021-07-14) - e74095d0</Val> </String> <String> <Name>Efuse Status</Name> <Val>02</Val> </String> <String> <Name>SCC Boot Version</Name> <Val>BM_OTA1_2022.42.7</Val> </String> </LVData>
2、winform界面见下图: