Solved

get single data from xml file

Posted on 2012-04-08
13
224 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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