Solved

get single data from xml file

Posted on 2012-04-08
13
222 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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