Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

get single data from xml file

Posted on 2012-04-08
13
Medium Priority
?
227 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…

610 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