C#读取XML文件
发布日期:2021-05-09 06:18:04 浏览次数:17 分类:博客文章

本文共 7309 字,大约阅读时间需要 24 分钟。

如下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文件的程序猿们。。可供参考

  

转载地址:https://www.cnblogs.com/ryzen/p/8143936.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:(转)MySql中监视增删改查和查看日志记录
下一篇:FileSystemWatcher类监控文件的更改状态并且实时备份文件

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年07月01日 02时16分36秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章