Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 21, 2013 12:28 PM by ramanselva
Participant
817 Points
243 Posts
Feb 21, 2013 06:06 AM|LINK
I have method where datatable is loaded with XML from database (only one value i.e XML is returned) Now I want to read XML Attribute from datatable (without converting to XMLDocument or XDocument) and then assign to Object for a class
Example : XML loaded in DATATABLE is
<Employee> <Name>Andy</Name> <Age>22</Age> <Place>Canada</Place> <Employer>IBM</Employer>
Now there is class Employee
public class Employee { public string Name {get;set;} public int Age {get;set;} public string Place {get;set;} public string Employer {get;set;} }
So the attribute I get from Datatable.row[0] , I need to assign to corresponding object property
Employee emp = new Employee();
emp.Name = "Andy"; emp.Age = 22; emp.Place = "Canada"; emp.Employer = "IBM";
NOTE: I need to do all this without going for either XMLDocument or Xdocument. Please help
Contributor
2064 Points
324 Posts
Feb 21, 2013 12:28 PM|LINK
Hi,
In this case you need to deserialize xml string back to the object type Employee,as given in example
http://dotnet.dzone.com/articles/serializing-and-deserializing
e.g:
City city1 = new City(); string inputString = <XML STRING>; MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString)); XmlSerializer serializer1 = new XmlSerializer(city1.GetType()); object deserialized1 = serializer1.Deserialize(memStream); city1 = (City)deserialized1;
http://www.c-sharpcorner.com/UploadFile/chauhan_sonu57/SerializingObjects07202006065806AM/SerializingObjects.aspx
jassu107
Participant
817 Points
243 Posts
Read XML Element Attribute from Datatable
Feb 21, 2013 06:06 AM|LINK
I have method where datatable is loaded with XML from database (only one value i.e XML is returned)
Now I want to read XML Attribute from datatable (without converting to XMLDocument or XDocument) and then assign to Object for a class
Example :
XML loaded in DATATABLE is
<Employee>
<Name>Andy</Name>
<Age>22</Age>
<Place>Canada</Place>
<Employer>IBM</Employer>
Now there is class Employee
public class Employee
{
public string Name {get;set;}
public int Age {get;set;}
public string Place {get;set;}
public string Employer {get;set;}
}
So the attribute I get from Datatable.row[0] , I need to assign to corresponding object property
Employee emp = new Employee();
emp.Name = "Andy";
emp.Age = 22;
emp.Place = "Canada";
emp.Employer = "IBM";
NOTE: I need to do all this without going for either XMLDocument or Xdocument. Please help
Mark as Answer if it helps you...
ramanselva
Contributor
2064 Points
324 Posts
Re: Read XML Element Attribute from Datatable
Feb 21, 2013 12:28 PM|LINK
Hi,
In this case you need to deserialize xml string back to the object type Employee,as given in example
http://dotnet.dzone.com/articles/serializing-and-deserializing
e.g:
City city1 = new City();
string inputString = <XML STRING>;
MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString));
XmlSerializer serializer1 = new XmlSerializer(city1.GetType());
object deserialized1 = serializer1.Deserialize(memStream);
city1 = (City)deserialized1;
http://www.c-sharpcorner.com/UploadFile/chauhan_sonu57/SerializingObjects07202006065806AM/SerializingObjects.aspx
This can be beneficial to other community members reading the thread.
Regards,
Rama Selvam M.