Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>


<Employees>
<Employee>
    <ID>104</ID>
    <Name>Sumit</Name>
<<Image>Bytes.............</Image>
  </Employee>
  <Employee>
    <ID>105</ID>
    <Name>Ravi</Name>

<Image>Bytes.............</Image>
  </Employee>
</Employees>


code :-


private void button2_Click(object sender, EventArgs e)
      {
          XDocument doc = XDocument.Load("XMLFile/PrintLabelList.xml");
          var records = (from lablemname in doc.Root.Elements("Employee")
                         select new
                         {
                             Image = (string)lablemname.Element("Image").Value
                         }).FirstOrDefault();
          if (records != null)
          {
              string val = records.Image;
              byte[] bytes = Convert.FromBase64String(val);
              MemoryStream mem = new MemoryStream(bytes);
              Bitmap bmp2 = new Bitmap(mem);
              pictureBox1.Image = bmp2;
          }
      }


What I have tried:

How to get specific image from xml file
Posted
Updated 13-Feb-19 21:01pm

1 solution

You must grab some more fields from your XML document.
Here also ID and Name are collected.
XDocument doc = XDocument.Load("XMLFile/PrintLabelList.xml");
var records = (from lablemname in doc.Root?.Elements("Employee")
   select new
   {
      ID = lablemname.Element("ID")?.Value,
      Name = lablemname.Element("Name")?.Value,
      Image = lablemname.Element("Image")?.Value,                   
   });

You can then easily get any wished item e.g. by its ID
var item = records.FirstOrDefault(x => x.ID == "104");

The image bytes can be accessed like this:
var imageBytes = item?.Image;
 
Share this answer
 
v2
Comments
Nishant.Chauhan80 14-Feb-19 10:53am    
Thanks sir
TheRealSteveJudge 15-Feb-19 2:08am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900