Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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
0
ASPNET_8
Asked:
ASPNET_8
1 Solution
 
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
 
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
 
ASPNET_8Author Commented:
Good
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now