XML to List of Class C#

if we have XML file like this :

<picturecollection>
  <photo>
    <name>One</name>
    <path>somepath1</path>
  </photo>
  <photo>
    <name>Two</name>
    <path>somepath2</path>
  </photo>
</picturecollection>


<picturecollection>
  <photo>
    <name>One</name>
    <path>somepath1</path>
  </photo>
  <photo>
    <name>Two</name>
    <path>somepath2</path>
  </photo>
</picturecollection>
  1. <code>
  2. <picturecollection>
  3. <photo>
  4. <name>One</name>
  5. <path>somepath1</path>
  6. </photo>
  7. <photo>
  8. <name>Two</name>
  9. <path>somepath2</path>
  10. </photo>
  11. </picturecollection>
  12. </code>
<code>
<picturecollection>
  <photo>
    <name>One</name>
    <path>somepath1</path>
  </photo>
  <photo>
    <name>Two</name>
    <path>somepath2</path>
  </photo>
</picturecollection>


</code>

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();
<div style="overflow-x: scroll;"> <div style="width: 1324px;"> XDocument xdoc = XDocument.Load(Server.MapPath(@"\Photo.xml")); List<pic> pics = (from xml in xdoc.Elements("PictureCollection").Elements("Photo") select new Pic { PicName = xml.Element("name").Value, TbPath = xml.Element("path").Value }).ToList(); </pic></div> </div>
  1. <code>
  2. <div style="overflow-x: scroll;">
  3. <div style="width: 1324px;">
  4. XDocument xdoc = XDocument.Load(Server.MapPath(@"\Photo.xml"));
  5. List<pic> pics = (from xml in xdoc.Elements("PictureCollection").Elements("Photo")
  6. select new Pic
  7. {
  8. PicName = xml.Element("name").Value,
  9. TbPath = xml.Element("path").Value
  10. }).ToList();
  11. </pic></div>
  12. </div>
  13. </code>
<code>
<div style="overflow-x: scroll;">
<div style="width: 1324px;">
            XDocument xdoc = XDocument.Load(Server.MapPath(@"\Photo.xml"));
            List<pic> pics = (from xml in xdoc.Elements("PictureCollection").Elements("Photo")
                              select new Pic
                              {
                                  PicName = xml.Element("name").Value,
                                  TbPath = xml.Element("path").Value
                              }).ToList();

</pic></div>
</div>
</code>


        public class Pic
        {
            public string PicName {get; set;}
            public string TbPath { get; set; }
        }
        public class Pic
        {
            public string PicName {get; set;}
            public string TbPath { get; set; }
        }
  1. <code>
  2. public class Pic
  3. {
  4. public string PicName {get; set;}
  5. public string TbPath { get; set; }
  6. }
  7. </code>
<code>
        public class Pic
        {
            public string PicName {get; set;}
            public string TbPath { get; set; }
        }
</code>

Popular posts from this blog

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=xx.0.0.0, Culture=neutral, PublicKeyToken='xxx' or one of its dependencies.

C# Crop white space from around the image

The specified version string contains wildcards, which are not compatible with determinism.