?
Solved

get single data from xml file

Posted on 2012-04-08
13
Medium Priority
?
225 Views
Last Modified: 2012-04-08
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 ....
0
Comment
Question by:Tech_Men
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37821794
0
 

Author Comment

by:Tech_Men
ID: 37821810
hi thanks for your answer
but i want something more simple
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 37821814
object obj = ConfigurationManager.AppSettings["ServerName"];
 if (obj == null) return;
string serverName = obj.ToString();
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:Tech_Men
ID: 37821820
hi MajorBigDeal
this is not a app.config file
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37821821
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
 

Author Comment

by:Tech_Men
ID: 37821822
u can change it if u like i just need to be able to read 1 single row
0
 

Author Comment

by:Tech_Men
ID: 37821825
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37821831
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
 

Author Comment

by:Tech_Men
ID: 37821832
i change the xml file to :

<SetData>
  <info>
    <ServerName>USER-PC\SQLX2008</ServerName>
  </info>
</SetData>
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 2000 total points
ID: 37821834
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
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 37821838
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
 

Author Closing Comment

by:Tech_Men
ID: 37821845
its work but i needed to change your code to this :

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

thanks ...
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 37821850
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question