简体   繁体   中英

XDocument load multiple XML

I have an ASP.Net webform app.
I have one fileupload in page allow multiple files.
In a foreach loop I try to read all XML the user uploaded to the folder.
The problem occurs if I read the documents. I only get the first XML in the selection. I simple use XDocument.load(filename) in a foreach. What did I wrong?

I fire that loop in a button click event

protected void btnSaveDatabase_Click(object sender, EventArgs e)
{   //hiddenfield in page have name of xml in folder
    var filesUploaded = hfXMLuploaded.Value.ToString().Split('-');
    try
    {        
        foreach (var file in filesUploaded)
        {
            string filename = Server.MapPath("~/SFA_XML_Upload/" + file);
            var doc = XDocument.Load(filename);
            Response.Write(doc.ToString()); // for testing

            //SaveDataBaseMyTable(ReadMyTable(doc, filename));                
        }

my read method:

protected  List<SFA_ORDCLI> ReadOrdcli(XDocument doc, string filename)
{
    var ordcli = doc.Descendants("table")
                   .Where(i => (string)i.Attribute("name") == "ordcli")
                   .Descendants("row")
                   .Select(e => e.Elements());

    List<SFA_ORDCLI> sfaOrdcliList = new List<SFA_ORDCLI>();
    foreach (var row in ordcli)
    {
        SFA_ORDCLI sfaOrdcli = new SFA_ORDCLI();
        sfaOrdcli.NomeFileXML = Path.GetFileNameWithoutExtension(filename);
        foreach (var field in row)
        {
            var name = (string)field.Attribute("name");

            switch (name)
            {
                case "RolCodEst"://key
                    sfaOrdcli.RolCodEst = (string)field;
                    break;
 ...other fields

            }
        }
        if (sfaOrdcli != null) sfaOrdcliList.Add(sfaOrdcli);
    }

    return sfaOrdcliList;

}

in debug i see Load method receive the files name 'name.xml' but the response print two time the first xml read.

Problem was on SaveAs method:

i did fileUpload.Saveas instead of file.SaveAs(folderPath + Path.GetFileName(file.FileName)); where file is HttpPostedFile

sorry bye and tks everybody

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM