REading XML Tags Values

T

TARUN

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top