Data from Xml to textboxes

hI

I am getting this string from Database
<Address>
<FirstName>Test</FirstName>
<LastName>Test</LastName>
<Line1>W62 N692 Riveredge Drive</Line1>
</Address>

I just want to save the value of different nodes to the respective textboxes in ASP.NET using C#.
What would be the best method to do that??

Thanks
ASPNET_8Asked:
Who is Participating?
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, these are examples of how to do that based on the eridanix recommendation:

By using Linq to handle xml (Framework 3.5 and later):
string xml = "<Address><FirstName>Test 1</FirstName><LastName>Test 2</LastName><Line1>W62 N692 Riveredge Drive</Line1></Address>";
System.Xml.Linq.XElement address = System.Xml.Linq.XElement.Parse(xml);
string firstName = address.Element("FirstName").Value;
string lastName = address.Element("LastName").Value;
string line1 = address.Element("Line1").Value;

Open in new window


By using the System.Xml.XmlDocument class:
string xml = "<Address><FirstName>Test 1</FirstName><LastName>Test 2</LastName><Line1>W62 N692 Riveredge Drive</Line1></Address>";
System.Xml.XmlDocument xmlDocAddress = new System.Xml.XmlDocument();
xmlDocAddress.LoadXml(xml);
string firstName = xmlDocAddress.SelectSingleNode("Address/FirstName").InnerText;
string lastName = xmlDocAddress.SelectSingleNode("Address/LastName").InnerText;
string line1 = xmlDocAddress.SelectSingleNode("Address/Line1").InnerText;

Open in new window

0
 
eridanixCommented:
Hi,

you can use Linq to XML.
http://msdn.microsoft.com/en-us/library/bb387098.aspx


Or the other posible solution is to parse XML using XmlDocument methods from System.Xml namespace.
Good example is here:
http://weblogs.asp.net/karan/archive/2010/04/29/parse-an-xml-file.aspx
0
 
ASPNET_8Author Commented:
Good
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.