如何在不使用System.Configuration的情况下以编程方式反序列化xml文件

时间:2019-01-19 06:27:23

标签: c# xml

我有带有此代码的xml文件

<configuration>

  <startup>
       <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> 
  </startup>

  <applicationSettings>
       <GlobalERP.UI.Properties.Settings>
           <setting name="SQLServerIP" serializeAs="String">
              <value>192.168.1.1</value>
           </setting>
           <setting name="SQLUserName" serializeAs="String">
              <value>sa</value>
           </setting>
           <setting name="SQLPassword" serializeAs="String">
              <value>123</value>
           </setting>
           <setting name="SQLDBName" serializeAs="String">
               <value />
           </setting>
       </GlobalERP.UI.Properties.Settings>
   </applicationSettings>
</configuration>

如何通过代码反序列化并通过“ SQLServerIP”或“ SQLUserName”之类的名称获取值并读取没有System.Configuration的行?

1 个答案:

答案 0 :(得分:0)

我觉得最好 Answer 进行少量修改

 XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList elemList = doc.GetElementsByTagName("setting");
            for (int i = 0; i < elemList.Count; i++)
            {
                if (elemList[i].Attributes["name"].Value == "UserName")
                {
                    textBox1.Text += elemList[i].InnerText;
                }
            }