Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to call the xml data in MVC view to display it but get the exception error. Any help will be much appreciated.

What I have tried:

--------XML----
XML
<root>
    <states>
        <state>
            <statename>Abia</statename>
            <stateid>101</stateid>
        </state> 
        <state>
            <statename>Adamawa</statename>
            <stateid>102</stateid>
        </state> 
        <state>
            <statename>Akwa Ibom</statename>
            <stateid>103</stateid>
        </state> 
        ...
    </states>
</root>


-----C# code---
C#
public class XMLREADER
{
    public List<Products> ReturnListOfProducts()
    {
        string xmlData = HttpContext.Current.Server.MapPath("~/App_Data/ProductData.xml");//Path of the xml script  
        DataSet ds = new DataSet();//Using dataset to read xml file  
        ds.ReadXml(xmlData);
        
        var products = new List<Products>();
        products = (from rows in ds.Tables[0].AsEnumerable()
                    select new Products
                    {       
                        StateName = rows[0].ToString(),
                        StateId = Convert.ToInt32(rows[1].ToString()),
                    }).ToList();
        
        return products;
    }
}
Posted
Updated 5-Sep-16 3:54am
v3
Comments
njammy 5-Sep-16 5:55am    
Which line is causing the exception?
Kasar_7 5-Sep-16 6:07am    
select new Products
{

StateName = rows[0].ToString(),
StateId = Convert.ToInt32(rows[1].ToString()),
}).ToList();
return products;
Those line since i can't find the rows/data from xml.
i believe it can be xml root problem.i have two root like
<--root1-->
<--states-->
<--state-->
<--statename-->abc<--/statename--><--stateid-->1<--/stateid-->
<--/state-->
<--/states-->

PS: Iam just putting here <--> since without that the root tag wont show
njammy 5-Sep-16 6:36am    
When you debug up to the line
ds.ReadXml(xmlData);
What does the DataSet look like? (how many tables, rows etc)
Patrice T 5-Sep-16 16:36pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Karthik_Mahalingam 5-Sep-16 6:42am    
validate the rows count before usage.

1 solution

Debug your code and examine the DataSet. You will see that the first table only has a single column - states_Id. The data you're trying to read is in the second table.
C#
public List<Products> ReturnListOfProducts()
{
    string xmlData = HttpContext.Current.Server.MapPath("~/App_Data/ProductData.xml");
    DataSet ds = new DataSet();
    ds.ReadXml(xmlData);
    
    return (from rows in ds.Tables[1].AsEnumerable()
            select new Products
            {       
                StateName = rows[0].ToString(),
                StateId = Convert.ToInt32(rows[1]),
            }).ToList();
}

You could have found this out for yourself much faster by using the debugger.
 
Share this answer
 

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