0

I am getting xml data from web api controller in c#. I need to show this data as list in html view page. I tried various methods but none of them is working. Here is the XML Data which i am getting from api. Suggest a way how it can be done. Thanks in advance.

<ArrayOfIDValue xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/IFlicksAPI.Controllers">
  <IDValue>
    <ID>1</ID>
    <Name>Shuba</Name>
  </IDValue>
  <IDValue>
    <ID>2</ID>
    <Name>Raji</Name>
 </IDValue>
 <IDValue>
   <ID>3</ID>
   <Name>Renu</Name>
 </IDValue>
</ArrayOfIDValue>

2 Answers 2

1

This way you can parse the xml and iterate through all the items

success: function(xml) {
            $(xml).find('IDValue').each(function(){
                var id = $(this).find("ID").text();
                var name = $(this).find("name").text();
                $("#list").append("<option value='" + id + "'>" + name + "</option>")
            });
        }

ofcource you need to use jQuery ajax function with your web api url

Sign up to request clarification or add additional context in comments.

Comments

0

You can create a class using xml.serialiation and serialize the xml data in to an object of that class, then just fill the list as it must be done

example: the xml template class

using System.Xml.Serialization;

namespace App
{
[XmlRoot("GuiConfig")]
public class ConfigParameters
{
    [XmlElement("field1")]
    public string field1;

    [XmlElement("field2")]
    public string field2;
}

how to serialize

public static void GetXmlData() 
    {
        config = new ConfigParameters(); //global scope var
        try
        {
            if (File.Exists("C:/path/config.xml"))
            {
                String xmlDoc = XDocument.Load("C:/path/config.xml").ToString();
                XmlSerializer serializer = new 
XmlSerializer(typeof(ConfigParameters));
                using (TextReader reader = new StringReader(xmlDoc))
                {
                    mainForm.config = 
(ConfigParameters)serializer.Deserialize(reader);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.