XDocument加载多个XML

时间:2018-04-29 17:12:14

标签: c# xml

我有一个ASP.Net webform应用程序 我在页面中有一个文件上载允许多个文件 在foreach循环中,我尝试读取用户上传到该文件夹​​的所有XML 如果我阅读文档,就会出现问题。我只选择了第一个XML。我在foreach中简单地使用XDocument.load(filename)。我错了什么?

我在按钮点击事件中触发该循环

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));                
        }

我的阅读方法:

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;

}
在调试中我看到Load方法接收文件名&#39; name.xml&#39; 但响应打印两次第一次xml读取。

1 个答案:

答案 0 :(得分:0)

SaveAs方法存在问题:

我做了fileUpload.Saveas而不是 file.SaveAs(folderPath + Path.GetFileName(file.FileName)); 文件是HttpPostedFile

抱歉 再见,并祝大家