c# 读取XML文件的示例

这篇文章主要介绍了c# 读取XML文件的示例,帮助大家更好的理解和使用c# 编程语言,感兴趣的朋友可以了解下。

如下XML文件:(算是一个属性值比较多的xml文件。。。读取该Xml算是我在公司实际的一个任务)

   

创建一个类WriteXml用来封装读取Xml的和属性值方法:代码如下

 class ReadXml { ///  /// 第一根节点的属性 ///  public string PrintNumber { get; set; } public string ControlBarcode { get; set; } public string ControlEBarcode { get; set; } public string ControlPIX { get; set; } public string PrinterPort { get; set; } public string isShowAreaName { get; set; } public string PrinterPortData { get; set; } public string SparePrinterNumber { get; set; } public string SparePrinterNumber2 { get; set; } public string ProductionAreaName { get; set; } public string PrintType { get; set; } public string PrintDevType { get; set; } public string logicalDpiW { get; set; } public string logicalDpiH { get; set; } public string Baudrate { get; set; } public string QZHEIGHT { get; set; } public string QZwaittime { get; set; } public string FILEwaittime { get; set; } public string enablestwoway { get; set; } public string CommandType { get; set; } ///  /// 第二根节点的属性 ///  public string printMode { get; set; } public string PrintTimeConfig { get; set; } public string FilterEmptyRows { get; set; } public string PrintFilePath { get; set; } public string BackFilePath { get; set; } public string readBOHPrintConfig { get; set; } public string CondingFormat { get; set; } public string isShowChangeBill { get; set; } public string isRunUpgrade { get; set; } public string SingleTurnSpaceTime { get; set; } public string storeprnPath { get; set; } public string FtpPort { get; set; } public string BCode { get; set; } public string UploadVersionAddress { get; set; } public string UploadVersionPort { get; set; } public void read() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"D:\Choic\PrintConfig.xml");//读取xml文件 XmlNode xn = xmlDoc.SelectSingleNode("serverset"); XmlNodeList xnlNL = xn.SelectNodes("devset");//得到根节点 foreach (XmlNode xnl in xnlNL)//遍历devset { WriteAndRead wr = new WriteAndRead(); XmlElement xe = (XmlElement)xnl; wr.PrintNumber = xe.GetAttribute("PrintNumber").ToString(); wr.ControlBarcode = xe.GetAttribute("ControlBarcode").ToString(); wr.ControlEBarcode = xe.GetAttribute("ControlEBarcode").ToString(); wr.ControlPIX = xe.GetAttribute("ControlPIX").ToString(); wr.PrinterPort = xe.GetAttribute("PrinterPort").ToString(); wr.isShowAreaName = xe.GetAttribute("isShowAreaName").ToString(); wr.PrinterPortData = xe.GetAttribute("PrinterPortData").ToString(); wr.SparePrinterNumber = xe.GetAttribute("SparePrinterNumber").ToString(); wr.SparePrinterNumber2 = xe.GetAttribute("SparePrinterNumber2").ToString(); wr.ProductionAreaName = xe.GetAttribute("ProductionAreaName").ToString(); wr.PrintType = xe.GetAttribute("PrintType").ToString(); wr.PrintDevType = xe.GetAttribute("PrintDevType").ToString(); wr.logicalDpiW = xe.GetAttribute("logicalDpiW").ToString(); wr.logicalDpiH = xe.GetAttribute("logicalDpiH").ToString(); wr.Baudrate = xe.GetAttribute("Baudrate").ToString(); wr.QZHEIGHT = xe.GetAttribute("QZHEIGHT").ToString(); wr.QZwaittime = xe.GetAttribute("QZwaittime").ToString(); wr.FILEwaittime = xe.GetAttribute("FILEwaittime").ToString(); wr.enablestwoway = xe.GetAttribute("enablestwoway").ToString(); wr.CommandType = xe.GetAttribute("CommandType").ToString(); Console.WriteLine("PrintNumber:" + wr.PrintNumber + "\tControlBarcode:" + wr.ControlBarcode + "\tControlEBarcode:" + wr.ControlEBarcode + "\tControlPIX:" + wr.ControlPIX + "\tPrinterPort:" + wr.PrinterPort + "\tisShowAreaName:" + wr.isShowAreaName + "\tPrinterPortData:" + wr.PrinterPortData + "\tSparePrinterNumber:" + wr.SparePrinterNumber + "\tSparePrinterNumber2:" + wr.SparePrinterNumber2 + "\tProductionAreaName:" + wr.ProductionAreaName + "\tPrintType:" + wr.PrintType + "\tPrintDevType:" + wr.PrintDevType + "\tlogicalDpiW:" + wr.logicalDpiW + "\tlogicalDpiH:" + wr.logicalDpiH + "\tBaudrate:" + wr.Baudrate + "\tQZHEIGHT:" + wr.QZHEIGHT + "\tQZwaittime:" + wr.QZwaittime + "\tFILEwaittime:" + wr.FILEwaittime + "\tenablestwoway:" + wr.enablestwoway + "\tCommandType:" + wr.CommandType); Console.WriteLine(); } XmlNodeList xnlNL1 = xn.SelectNodes("OrtherInfo");//得到第二个根节点 foreach (XmlNode xnl in xnlNL1)//OrtherInfo { WriteAndRead wr = new WriteAndRead(); XmlElement xe = (XmlElement)xnl; wr.printMode = xe.GetAttribute("printMode").ToString(); wr.PrintTimeConfig = xe.GetAttribute("PrintTimeConfig").ToString(); wr.FilterEmptyRows = xe.GetAttribute("FilterEmptyRows").ToString(); wr.PrintFilePath = xe.GetAttribute("PrintFilePath").ToString(); wr.BackFilePath = xe.GetAttribute("BackFilePath").ToString(); wr.readBOHPrintConfig = xe.GetAttribute("readBOHPrintConfig").ToString(); wr.CondingFormat = xe.GetAttribute("CondingFormat").ToString(); wr.isShowChangeBill = xe.GetAttribute("isShowChangeBill").ToString(); wr.isRunUpgrade = xe.GetAttribute("isRunUpgrade").ToString(); wr.SingleTurnSpaceTime = xe.GetAttribute("SingleTurnSpaceTime").ToString(); wr.storeprnPath = xe.GetAttribute("storeprnPath").ToString(); wr.FtpPort = xe.GetAttribute("FtpPort").ToString(); wr.UploadVersionAddress = xe.GetAttribute("UploadVersionAddress").ToString(); wr.UploadVersionPort = xe.GetAttribute("UploadVersionPort").ToString(); Console.WriteLine("printMode:" + wr.printMode + "\tPrintTimeConfig:" + wr.PrintTimeConfig + "\tFilterEmptyRows:" + wr.FilterEmptyRows + "\tPrintFilePath:" + wr.PrintFilePath + "\tBackFilePath:" + wr.BackFilePath + "\treadBOHPrintConfig:" + wr.readBOHPrintConfig + "\tCondingFormat:" + wr.CondingFormat + "\tisShowChangeBill:" + wr.isShowChangeBill + "\tisRunUpgrade:" + wr.isRunUpgrade + "\tSingleTurnSpaceTime:" + wr.SingleTurnSpaceTime + "\tstoreprnPath:" + wr.storeprnPath + "\tFtpPort:" + wr.FtpPort + "\tUploadVersionAddress:" + wr.UploadVersionAddress + "\tUploadVersionPort:" + wr.UploadVersionPort); } }

在控制台上运行。。

 class Program { static void Main(string[] args) { ReadXml RX = new ReadeXml(); RX.Read(); Console.Read(); } }

运行结果如下:

好了,以上就是读取该XML文件以及实际运行结果图。。。。。。。

大牛们就当看个笑话啦!当然有哪里不对的地方或者可以改进的方法可以提出来。。。。对于一些没用读取过XML文件的程序猿们。。可供参考

以上就是c# 读取XML文件的示例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程