By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,217 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,217 IT Pros & Developers. It's quick & easy.

REading XML Tags Values

P: n/a
Hello All

This is my XML File,

<?xml version="1.0" encoding="utf-8" ?>
- <Authentiation>
<userId>TarunSinha</userId>
<password>sonumonu</password>
</Authentiation>
I want to read the XML Tag's value and compare those value with str1
and str2 respectively
in C#.NET
Can Anyone suggest me the Code for it.

With regards
Tarun Sinha

May 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Tarun,

In order to read from (and write to) Xml streams, ensure that the System.Xml
assembly is referenced in your project. Also do not forget to include the
System.Xml namespace in your code (if not included already).

Provided that the root element's name is 'Authentiation', and the Xml file
is saved to a local file C:\Sample.Xml, you can use the following code
snippet to obtain the UserId and Password values:

string strUserName = string.Empty, strPassword = string.Empty;
XmlNode xnAuth, xnCur;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"C:\Sample.Xml");

xnAuth = xDoc.SelectSingleNode("Authentiation");
if (xnAuth != null)
{
xnCur = xnAuth.SelectSingleNode("userId");
if (xnCur != null)
strUserName = xnCur.InnerText;
xnCur = xnAuth.SelectSingleNode("password");
if (xnCur != null)
strPassword = xnCur.InnerText;
}

Checks are made to make sure that the appropriate Xml nodes exist
(otherwise, NullReferenceException would be thrown). The found values are
stored in strUserName and strPassword respectively. You can now compare the
values with the ones you wish.

--
Stanimir Stoyanov
www.stoyanoff.info
"TARUN" wrote:
Hello All

This is my XML File,

<?xml version="1.0" encoding="utf-8" ?>
- <Authentiation>
<userId>TarunSinha</userId>
<password>sonumonu</password>
</Authentiation>
I want to read the XML Tag's value and compare those value with str1
and str2 respectively
in C#.NET
Can Anyone suggest me the Code for it.

With regards
Tarun Sinha

May 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.