none
How do i loop through these images in my XML and place them in html <img> using StringBuilder in C#?

    Question

  • I want it to search for the id and when it finds it, it must loop through the BigPhoto and display all the images that it can find.

    I have this code

    public static void Main()
    	{
    		string str = @"<?xml version='1.0'?>
    						<information>
    							<details>
    								<id>01</id>
    								<empname>qwer</empname>
    								<empid>001</empid>
    								<dept>Accounts</dept>
    								<date>2011-01-15 12:21:25</date>
    								<photo>
    									<BigPhoto>Foto1</BigPhoto>
    									<BigPhoto>Foto1</BigPhoto>
    									<BigPhoto>Foto1</BigPhoto>
    									<SmallPhoto>Foto1</SmallPhoto>
    									<SmallPhoto>Foto1</SmallPhoto>
    									<SmallPhoto>Foto1</SmallPhoto>
    								</photo>
    							</details>
    							<details>
    								<id>02</id>
    								<empname>Feelie</empname>
    								<empid>002</empid>
    								<dept>Finance</dept>
    								<date>2011-01-20 10:21:25</date>
    								<photo>
    									<BigPhoto>Foto1</BigPhoto>
    									<BigPhoto>Foto1</BigPhoto>
    									<SmallPhoto>Foto1</SmallPhoto>
    									<SmallPhoto>Foto1</SmallPhoto>
    								</photo>
    							</details>
    						</information>";
    
    		var str1 = XElement.Parse(str);
    		
    		//Search ID
    		string searchID = "01";
    
    		//Get Result
    		var result = str1.Elements("details").Where(x => x.Element("id").Value.Equals(searchID)).ToList();
    		
    		if(result.Count() > 0)
    		{
    			//Get Specific element value
    			string s = result[0].Elements("empname").FirstOrDefault().Value;			
    			Console.WriteLine(s);
    		}
    	}

    Tuesday, April 4, 2017 9:57 AM

Answers

  • Here's a first pass assuming you don't care about anything other than the photo name (which is a string?). You can expand to meet your needs.

    static string BuildImages ( XElement xml, string id )
    {
        //Find element, if any
        var element = xml.XPathSelectElement($"details[id={id}]");
        if (element == null)
            return null;
    
        //Get the photos
        var photo = element.Element("photo");
        if (photo == null)
            return null;
    
        //Build the images
        var builder = new StringBuilder();
        foreach (var child in photo.Descendants())
        {
            //Build it up however you want here...
            builder.Append($"<img>{child.Value}</img>");
        };
    
        return builder.ToString();
    }
    
    //Usage
    var images = BuildImages(xml, "01");

    Michael Taylor
    http://www.michaeltaylorp3.net

    Tuesday, April 4, 2017 2:33 PM
    Moderator

All replies

  • Here's a first pass assuming you don't care about anything other than the photo name (which is a string?). You can expand to meet your needs.

    static string BuildImages ( XElement xml, string id )
    {
        //Find element, if any
        var element = xml.XPathSelectElement($"details[id={id}]");
        if (element == null)
            return null;
    
        //Get the photos
        var photo = element.Element("photo");
        if (photo == null)
            return null;
    
        //Build the images
        var builder = new StringBuilder();
        foreach (var child in photo.Descendants())
        {
            //Build it up however you want here...
            builder.Append($"<img>{child.Value}</img>");
        };
    
        return builder.ToString();
    }
    
    //Usage
    var images = BuildImages(xml, "01");

    Michael Taylor
    http://www.michaeltaylorp3.net

    Tuesday, April 4, 2017 2:33 PM
    Moderator
  • thank you very much
    Wednesday, April 5, 2017 11:02 AM