Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Helloo every one,
I'm new to xml file and I'm try to solve this problem but i'm struggling with it
please can anyone suggest any idea about it

I have this code
when I run this code it give me error in (ds.ReadXml(filePath);)

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/folder/") + filename);
                string filePath = "~/folder/" + filename;
                Label1.Text = "File uploaded successfully.";
                DataSet ds = new DataSet();
                ds.ReadXml(filePath);
                GridView1.Visible = true;
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                Label1.Text = "Please select file.";
            }
        }


What I have tried:

I'm try to upload xml file to folder
after it download to folder it should read the xml file and display it in Gridview
Posted
Updated 3-May-16 0:22am
Comments
George Jonsson 2-May-16 21:41pm    
What kind of error do you get?
Is the file missing or is the XML file corrupt or what?
Member 12292833 3-May-16 5:54am    
Fistly, it does not accept all xml file, I dont know why, it accept one file and other file didnot
and it give me this error
An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code

Additional information: Cannot add a nested relation or an element column to a table containing a SimpleContent column.
Karthik_Mahalingam 2-May-16 23:30pm    
Show the sample data in xml file.
what is the exact error message?
Member 12292833 3-May-16 5:54am    
An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code

Additional information: Cannot add a nested relation or an element column to a table containing a SimpleContent column.
Karthik_Mahalingam 3-May-16 6:01am    
need sample data

C#
using System.Data;

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath("~/Customers.xml"));
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}
 
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}



site that original code from Read XML File and bind it to GridView in ASP.Net[^]
 
Share this answer
 
v2
use this code

C#
string fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
               FileUpload1.SaveAs(Server.MapPath("~/folder/") + fileName);
               string filePath = "~/folder/" + filename;
               string filePath = Path.Combine(Server.MapPath("~/folder/"), fileName); // should get full path
               Label1.Text = "File uploaded successfully.";
 
Share this answer
 
It looks like you might have some XML files with a structure that is not supported by the DataSet class, but it is difficult to help you without seeing the actual XML file.

If it is very large you shouldn't post it here, instead open the XML file in a text editor and try to remove redundant data. Try to load it from time to time to see if you still have the problem. If not, then you kind of know where the problem lies.
If it is small enough you can update your question with the relevant XML data.

Also look for recursive nesting inside the XML file. DataSet doesn't like that very much.
Example:
XML
<root>
    <node>
        <name>Harry Hacker</name>
        <node>
            <name>Mickey Mouse</name>
        </node>
    </node>
</root>
 
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