get single data from xml file

hi there

this is my xml file content :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <add key="ServerName" value="SERVER=USER-PC\SQLX2008"/>
</configuration>

i want to read the value in c# to a textbox
how can i do it ?
thanks ....
Tech_MenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

basicinstinctCommented:
0
Tech_MenAuthor Commented:
hi thanks for your answer
but i want something more simple
0
MajorBigDealCommented:
object obj = ConfigurationManager.AppSettings["ServerName"];
 if (obj == null) return;
string serverName = obj.ToString();
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tech_MenAuthor Commented:
hi MajorBigDeal
this is not a app.config file
0
BuggyCoderCommented:
using LINQ to XML, here is how i would do this:-

var xmlData =
                "<?xml version='1.0' encoding='utf-8' ?><configuration><add key='ServerName' value='SERVER=USER-PC\\SQLX2008'/></configuration>";

            var element = XElement.Parse(xmlData).Descendants("add").Attributes("value").First().Value;

Open in new window

0
Tech_MenAuthor Commented:
u can change it if u like i just need to be able to read 1 single row
0
Tech_MenAuthor Commented:
hi BuggyCoder
i need to read data from xml file i dont have the data like in your answer

var xmlData =
                "<?xml version='1.0' ..........
0
BuggyCoderCommented:
use this then:-
var element = XElement.Load("<Path_To_Your_XMLFile>");
var serverValue=element.Descendents("add").Attributes("value").First().Value;

Open in new window


Namespace to use : System.Xml.Linq;
0
Tech_MenAuthor Commented:
i change the xml file to :

<SetData>
  <info>
    <ServerName>USER-PC\SQLX2008</ServerName>
  </info>
</SetData>
0
BuggyCoderCommented:
here is the code to use for your modified xml file:-

var element = XElement.Load("<Path_To_Your_XMLFile>");
var value = element.Descendants("ServerName").First().Value;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MajorBigDealCommented:
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNodeList nodes = doc.GetElementsByTagName("Add");
foreach (XmlNode n1 in nodes[0])
{
XmlNode xKey = n1.Attributes["key"];
string key = (xKey == null) ? string.Empty : xKey.Value.Trim();
XmlNode xValue = n1.Attributes["value"];
string value = (xValue == null) ? string.Empty : xValue.Value.Trim();

}
0
Tech_MenAuthor Commented:
its work but i needed to change your code to this :

var element = XElement.Load("Control.xml");
txtIp.Text = element.Value;

thanks ...
0
MajorBigDealCommented:
I wrote that on the fly, I see that the string "Add" should not be capitalized and the foreach should not have  index zero on the nodes object
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.