XML to List of Class C#
if we have XML file like this :
we can use This Code to return List of Class
<picturecollection>
<photo>
<name>One</name>
<path>somepath1</path>
</photo>
<photo>
<name>Two</name>
<path>somepath2</path>
</photo>
</picturecollection>
we can use This Code to return List of Class
XDocument xdoc = XDocument.Load(Server.MapPath(@"\Photo.xml"));
List pics = (from xml in xdoc.Elements("PictureCollection").Elements("Photo")
select new Pic
{
PicName = xml.Element("name").Value,
TbPath = xml.Element("path").Value
}).ToList();
public class Pic
{
public string PicName {get; set;}
public string TbPath { get; set; }
}
Comments
Post a Comment