Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi this is my xml

<pre lang="text">

<request action="registration">
<element id="id001">
<properties name="username">DV_sonic78
<properties name="password">prabu78



I want to take username value as DV_sonic78 and password as prabu78 from this XML

What I have tried:

<pre lang="c#"> XmlDocument doc = new XmlDocument();
                     doc.LoadXml(StrRequest);
                     XmlElement root = doc.DocumentElement;
                     string s = root.Attributes["username"].InnerText;
Posted
Updated 1-Nov-16 20:59pm
v3

Should be root.Attributes["username"].Value... and your XML look a little bit not well-formed :)
 
Share this answer
 
Change inner text to Value you will get the value of that attribute
string s = root.Attributes["username"].Value;
 
Share this answer
 
Comments
MalathiMals 2-Nov-16 3:11am    
object null reference exception coming
MJ2014 2-Nov-16 3:22am    
this is working code. Check your xml file , you might have missed something there
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Filepath);
XmlNodeList userNodes = xmlDoc.SelectNodes("//MainNode/Subnode1");
foreach (XmlNode userNode in userNodes)
{
string name= userNode.Attributes["Name"].Value;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900