欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 读取XML文件的示例

程序员文章站 2022-06-23 11:30:02
如下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的资料请关注其它相关文章!

相关标签: c# 读取 xml