zoukankan      html  css  js  c++  java
  • Xml配置文件

    最近做项目又遇到一个需要写配置文件的,懒得自己写就把之前写的一个简单的扒出来改了改,在此贴出来,省得以后还要重新写

    代码:

        private string appSettingName = "Settings.xml";
        private void InitSetting()
        {
            string path = Path.Combine(Application.streamingAssetsPath, appSettingName);
    
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
           
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("appSettings").ChildNodes;
            foreach (XmlNode temp in nodeList)
            {
                if(temp is XmlComment)
                {
                    continue; 
                }
                
                XmlElement xml = (XmlElement)temp;
                string key = xml.GetAttribute("key");
    
                if(key== "sensorPara")
                {
                    float f;
                    int i;
                    sensorPara.sensorHeight = float.TryParse(xml.GetAttribute("sensorHeight"), out f) ? f : 1f;
                    sensorPara.sensorAngle = float.TryParse(xml.GetAttribute("sensorAngle"), out f) ? f : 0f;
                    sensorPara.minDistance = float.TryParse(xml.GetAttribute("minDistance"), out f) ? f : 0.5f;
                    sensorPara.maxDistance = float.TryParse(xml.GetAttribute("maxDistance"), out f) ? f : 3.5f;
                    sensorPara.maxLRDistance = float.TryParse(xml.GetAttribute("maxLRDistance"), out f) ? f : 1f;
                    sensorPara.trackedUsers = int.TryParse(xml.GetAttribute("trackedUsers"), out i) ? i : 1;
                    continue;
                }
    
                string value = xml.GetAttribute("value");
                appParaKeys.Add(key);
                appParaValues.Add(value);
            }
    
            float t;
            time = float.TryParse(appParaValues[0], out t) ? t : 2f;         
        }

    配置文件:

    <?xml version="1.0" encoding="utf-8"?>
    <appSettings>
    <!--app_cursorTime时间-->
    <add key="app1_cursorTime" value="1.5"/>
    <!--app_web网址-->
    <add key="app2_web" value="https://www.baidu.com"/>
    <!--app_exe应用程序-->
    <add key="app3_video" value="D:	estVideo	est.mp4"/>
    <!--sensorPara传感器参数-->
    <add key="sensorPara" sensorHeight="2.2" sensorAngle="-15" minDistance="1" maxDistance="2" maxLRDistance="0.5" trackedUsers="1"/>
    </appSettings>
  • 相关阅读:
    Topo软件
    如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
    Web学习
    测试Web服务接口
    WebService
    记录几个博客
    Oracle日志性能查看
    设计模式
    hdu 1999 不可摸数
    parfor —— matlab 下的并行循环
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9417368.html
Copyright © 2011-2022 走看看