0

I have xml file with this code

<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>

How to deserialize and get value by name like "SQLServerIP" or "SQLUserName" by code and read lines without System.Configuration?

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
xxxsenatorxxx
  • 121
  • 2
  • 13
  • https://stackoverflow.com/questions/13043530/what-is-app-config-in-c-net-how-to-use-it – Mary Jan 19 '19 at 06:32
  • 1
    That's not well-formed XML, try uploading it to https://www.xmlvalidation.com and you will get errors. That being said, if you just need to quickly parse some XML and pick out some values you can use LINQ to XML; see [How does one parse XML files?](https://stackoverflow.com/q/55828/344280) or [LINQ to read XML](https://stackoverflow.com/q/670563/344280). – dbc Jan 19 '19 at 06:43
  • @dbc your link not work for me because i have n level in my xml file – xxxsenatorxxx Jan 19 '19 at 07:35
  • 1
    @xxxsenatorxxx - which link? I posted two. Anyway, using [LINQ to XML](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview) you can query deeply nested elements as shown in [LINQ to XML extract nested elements](https://stackoverflow.com/q/27985418), [LINQ to XML nested elements query](https://stackoverflow.com/q/25109640) or [Accessing nested elements while iterating an XML LINQ query?](https://stackoverflow.com/q/30958626). Maybe you could provide a [mcve] if those general answers aren't sufficient? – dbc Jan 19 '19 at 07:42
  • 1
    And if you want to query a nested element based on an attribute value (here `name="SQLServerIP"` for instance) see [LINQ to XML - Get element based on a nested elements value](https://stackoverflow.com/a/3941640) or [Parsing XML Files in .NET](https://stackoverflow.com/a/4326586). – dbc Jan 19 '19 at 07:51
  • As an alternative to LINQ to XML you could also use XPath – maracuja-juice Jan 19 '19 at 08:32

1 Answers1

0

I find Best Answer with an small edit

 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;
                }
            }
xxxsenatorxxx
  • 121
  • 2
  • 13