C# 读取.xml文件中的某一行报Null解决办法

需要读取的文件内容如下的“cameraCount”

<?xml version="1.0" encoding="utf-8"?>
<WindowConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <displayRow>1</displayRow>
  <displayColumn>1</displayColumn>
  <cameraCount>1</cameraCount>
</WindowConfig>

具体读写代码如下:

public int LoadConfigXml(int Count)//string configPath,int row,int count
{
    //object configXmlResult = null;
    string configXmlResult = null;
    XmlDocument xmlDocument = new XmlDocument();
    if (!Directory.Exists("config.xml"))
    {
        xmlDocument.Load(WindowConfig.filePath);//.xml路径

        XmlNode playerInfo = xmlDocument.SelectSingleNode("WindowConfig");//先获取根节点

        if (!窗体设置ToolStripMenuItem.Checked)//如果按下执行
        {
            XmlNode row = playerInfo.SelectSingleNode("displayRow");//获取根子节点
            XmlNode count = playerInfo.SelectSingleNode("displayColumn");
            //XmlNode mcameraCount = xmlDocument.SelectSingleNode("cameraCount");
            //将字符串转换未Int型
            rowParse = int.Parse(row.InnerText.ToString());
            countParse = int.Parse(count.InnerText.ToString());

            cameraCount = rowParse * countParse;
        }
        else//说明窗口设置按钮没有启用默认读取本地config.xml文件读取cameraCount数
        {
            //XmlNode mcameraCount = xmlDocument.SelectSingleNode("cameraCount");
            XmlNode mcameraCount = xmlDocument.DocumentElement.SelectSingleNode("cameraCount");
            cameraCount = Convert.ToInt16(mcameraCount.ToString());
        }

        WindowConfig.gOnly.cameraCount = cameraCount;
        WindowConfig.SaveConfig();//将窗体个数写入config.xml文件中
        MessageBox.Show(cameraCount.ToString());
    }
    else
    {
        MessageBox.Show("所加载的config.xml文件不存在");
    }

    WindowConfig.gOnly.cameraCount = cameraCount;//将计算的窗体个数存储
    return cameraCount;
}      

在这里插入图片描述
如果采用注释的那句代码读的话会导致null,正确的是采用未注释代码​:

//XmlNode mcameraCount = xmlDocument.SelectSingleNode("cameraCount");
XmlNode mcameraCount = xmlDocument.DocumentElement.SelectSingleNode("cameraCount");

正常代码如下:

XmlNode mcameraCount = xmlDocument.DocumentElement.SelectSingleNode("cameraCount");

附对​.xml文件进行读写保存操作的类如下:

using System;
using System.Collections.Generic;
using System.Diagnostics.PerformanceData;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;

namespace VMPro2024._08.Class
{
    public class WindowConfig
    {
        //窗体个数
        public int displayRow = 1;
        public int displayColumn = 1;
        public int cameraCount = 0;

        private static bool SaveToXml(string filePath,object sourceObj)
        {
            try
            {
                if(!string.IsNullOrWhiteSpace(filePath)&&sourceObj!=null)
                {
                    using(StreamWriter writer=new StreamWriter(filePath))
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(sourceObj.GetType());
                        xmlSerializer.Serialize(writer, sourceObj);
                    }
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            return true;
        }

        private static object LoadFormXml(string filePath)
        {
            object result = null;
            try
            {
                if(File.Exists(filePath))
                {
                    using(StreamReader reader=new StreamReader(filePath))
                    {
                        XmlSerializer xmlSerialize = new XmlSerializer(typeof(WindowConfig));
                        result = xmlSerialize.Deserialize(reader);
                    }
                }
            }
            catch(Exception e)
            {
                MessageBox.Show("配置文件不存在" + e.ToString());
            }
            return result;
        }

        //配置文件路径
        public static string filePath = System.Windows.Forms.Application.StartupPath + "\\" + "config.xml";

        //Vpp文件路径
        
        //保存配置文件
        public static bool SaveConfig()
        {
            SaveToXml(filePath, GetIntance());
            return true;
        }

        public static bool LoadConfig()
        {
            if(!File.Exists(filePath))
            {
                SaveToXml(filePath, GetIntance());
            }

            SetInstance((WindowConfig)LoadFormXml(filePath));
            return true;
        }

        public static WindowConfig gOnly = null;
        private static Object intanceLock = new object();

        public static void SetInstance(WindowConfig obj)
        {
            gOnly = null;
            gOnly = obj;
        }

        public static WindowConfig GetIntance()
        {
            if(gOnly==null)
            {
                lock(intanceLock)
                {
                    if (gOnly == null)
                        gOnly = new WindowConfig();
                }
            }

            return gOnly;
        }
    }
}

琐碎时间阅读基础知识,详情关注微信公众号“知识代码AI”。
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值