运维开发网

c# 读取XML文件的示例

运维开发网 https://www.qedev.com 2020-11-12 11:25 出处:网络 作者: RyzenAdorer
如下XML文件:(算是一个属性值比较多的xml文件。。。读取该Xml算是我在公司实际的一个任务)

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

<?xml version="1.0" encoding="UTF-8"?>
<serverset>
 <devset PrintNumber="1" ControlBarcode="" ControlEBarcode="" ControlPIX="" PrinterPort="0" isShowAreaName="0" PrinterPortData="127.0.0.1" SparePrinterNumber="1" SparePrinterNumber2="1" ProductionAreaName="1" PrintType="QT;1B;1K;1L;1M;" PrintDevType="0" logicalDpiW="" logicalDpiH="" Baudrate="4800" QZHEIGHT="1" QZwaittime="45" FILEwaittime="45" enablestwoway="1" CommandType="0"/>
 <devset PrintNumber="2" ControlBarcode="" ControlEBarcode="" ControlPIX="" PrinterPort="0" isShowAreaName="0" PrinterPortData="127.0.0.211" SparePrinterNumber="1" SparePrinterNumber2="1" ProductionAreaName="" PrintType="" PrintDevType="0" logicalDpiW="" logicalDpiH="" Baudrate="4800" QZHEIGHT="1" QZwaittime="45" FILEwaittime="45" enablestwoway="0" CommandType="0"/>
 <devset PrintNumber="3" ControlBarcode="" ControlEBarcode="" ControlPIX="" PrinterPort="0" isShowAreaName="0" PrinterPortData="127.0.0.3" SparePrinterNumber="1" SparePrinterNumber2="1" ProductionAreaName="" PrintType="" PrintDevType="0" logicalDpiW="" logicalDpiH="" Baudrate="4800" QZHEIGHT="1" QZwaittime="45" FILEwaittime="45" enablestwoway="0" CommandType="0"/>
 <devset PrintNumber="4" ControlBarcode="" ControlEBarcode="" ControlPIX="" PrinterPort="3" isShowAreaName="0" PrinterPortData="USB2" SparePrinterNumber="1" SparePrinterNumber2="1" ProductionAreaName="" PrintType="" PrintDevType="2" logicalDpiW="" logicalDpiH="" Baudrate="4800" QZHEIGHT="1" QZwaittime="45" FILEwaittime="45" enablestwoway="1" CommandType="0"/>
 <OrtherInfo printMode="1" PrintTimeConfig="5" FilterEmptyRows="0" PrintFilePath="D:/choice/ftp/tiktenPrint" BackFilePath="D:/choice/ftp/prnBakDir" readBOHPrintConfig="1" CondingFormat="0" isShowChangeBill="0" isRunUpgrade="0" SingleTurnSpaceTime="30" storeprnPath="127.0.0.1" FtpPort="" BCode="1020001" UploadVersionAddress="" UploadVersionPort="0"/>
</serverset>

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

class ReadXml
 {
  /// <summary>
  /// 第一根节点的属性
  /// </summary>
  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; }

  /// <summary>
  /// 第二根节点的属性
  /// </summary>

  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();
  }
 }

运行结果如下:

c# 读取XML文件的示例

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

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

以上就是c# 读取XML文件的示例的详细内容,更多关于c# 读取xml的资料请关注我们其它相关文章!

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号