How to read data from XML file
<?xml version="1.0" encoding="utf-8" ?>
<user>
<username>prince</username>
<password>user1</password>
</user>
This is my xml file name as user.xml.
Now when I click a button on the page, I need to get data from this file and put that data into a variable like:
string strusername = data cmg from xml file (prince)
string strPassword = data cmg from xml file (password)
Can anyone tell me how to do this with the syntax?
Thank you
+2
source to share
2 answers
LINQ to XML is the modern way to do what you want.
XDocument xDoc = XDocument.Load("user.xml");
string strusername = xDoc.Descendants(XName.Get("username")).First().Value;
string strPassword = xDoc.Descendants(XName.Get("password")).First().Value;
+6
source to share
You can try something like XmlReader , XmlTextReader and XmlDocument . They are all relatively straightforward.
0
source to share