通过C#中的WebBrowser控件下载XML文件

时间:2019-01-30 00:08:24

标签: c# winforms file download webbrowser-control

我有一个Windows窗体,该Web窗体提供了WebBrowser控件,我需要下载XML文件,但是“下载”按钮使用JavaScript即时生成了URI,因此我无法通过以下方式引用保存XML的URL / URI一行代码。因此,我所做的就是通过其ID抓取该元素,然后调用“ click”按钮。那不好,因为我需要知道我下载的XML文件的名称并知道它的去向,因此我可以引用该文件并进行解析。

这是按钮的HTML:

<button type="button" id="__button35" data-sap-ui="__button35" 
role="button" aria-disabled="false" tabindex="0" 
class="HB-14-1E3F57 extTextLinkDark sapUiBtn sapUiBtnIconAndText 
sapUiBtnNorm sapUiBtnS whiteToolPopupListLink sapUiBtnStd"><img 
id="__button35-img" src="costco/resource/image/xmlIcon.png" 
alt="" role="presentation" class="sapUiBtnIco sapUiBtnIcoL"><span 
class="sapUiBtnTxt">Download Details as XML</span></button>

这是单击按钮的简单方法:

var buttonElement = webBrowser1.Document.GetElementById("__button35");
buttonElement.InvokeMember("click");

有人可以帮我找到一种方法,将其发送到我选择的文件夹中,为xml文件指定一个不同的名称,或者甚至在下载时捕获它,以便我可以解析数据吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我通过取消Windows窗体并完全依赖于使用宏记录器,C#控制台应用程序和任务计划程序的自动化来解决了这一问题。首先,我创建了Macro Recorder,以登录网站并导航到相应的页面,选择要下载的最新日期,然后将XML File下载到正确的File文件夹中。然后,我创建了一个C#应用程序,该应用程序从文件位置中拉出了文件(我不得不将其作为字符串拉入,因为XML文件太大,任何其他方法都会截断数据并使文件成为无效的XML)并解析XML文件,将其加载到SQL Server数据库中,然后将文件存档。所有这些都是通过我的服务器任务计划程序完成的。这是来自C#主类的代码,这是我发现的解析大型XML文件并确保将每个值都放入数据库中的最佳方法(item标记启动子节点):

using System;
using System.Linq;
using System.IO;
using System.Xml;

namespace Parse_Returns_XML_To_SQL
{
    class Program
    {
    static void Main(string[] args)
    {
        int counter = 0;
        try
        {
            MainGetSet mainGetSet = new MainGetSet();
            LineGetSet lineGetSet = new LineGetSet();


            foreach (string file in Directory.EnumerateFiles(@"C:\_Data\CostcoXML", "*.xml"))
            {
                counter++;
                string xmlContents = File.ReadAllText(file);

                Console.WriteLine(file);
                //Console.ReadKey();


                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(xmlContents);
                XmlNodeList RTVnodes = xmldoc.SelectNodes("memo-list/RTVMemo");

                foreach (XmlNode rtv in RTVnodes)
                {
                    //MessageBox.Show(rtv["deduction-num"].InnerText);

                    if (rtv["vendor-num"].InnerText.Length > 0)
                    {
                        mainGetSet.VendorNum1 = rtv["vendor-num"].InnerText;
                    }
                    if (rtv["dept-num"].InnerText.Length > 0)
                    {
                        mainGetSet.DeptNum1 = rtv["dept-num"].InnerText;
                    }
                    if (rtv["deduction-num"].InnerText.Length > 0)
                    {
                        mainGetSet.DeductionNum1 = rtv["deduction-num"].InnerText;
                        lineGetSet.DeductionNum1 = rtv["deduction-num"].InnerText;
                    }
                    if (rtv["ra-num"].InnerText.Length > 0)
                    {
                        mainGetSet.RaNum1 = rtv["ra-num"].InnerText;
                        lineGetSet.RaNum1 = rtv["ra-num"].InnerText;
                    }
                    if (rtv["date-shipped"].InnerText.Length > 0)
                    {
                        mainGetSet.DateShipped1 = rtv["date-shipped"].InnerText;
                    }
                    if (rtv["shipping-loc"].InnerText.Length > 0)
                    {
                        mainGetSet.ShippingLoc1 = rtv["shipping-loc"].InnerText;
                    }
                    if (rtv["carrier-name"].InnerText.Length > 0)
                    {
                        mainGetSet.CarrierName1 = rtv["carrier-name"].InnerText;

                        if (mainGetSet.CarrierName1.Trim().Contains("FREIGHT"))
                        {
                            lineGetSet.TrackOrTruck1 = "Freight";
                        }
                        else if (mainGetSet.CarrierName1.Trim().Contains("VEND"))
                        {
                            lineGetSet.TrackOrTruck1 = "Vendor";
                        }
                        else
                        {
                            lineGetSet.TrackOrTruck1 = "Track";
                        }

                    }
                    if (rtv["total-cases"].InnerText.Length > 0)
                    {
                        mainGetSet.TotalCases1 = rtv["total-cases"].InnerText;
                    }
                    if (rtv["total-weight"].InnerText.Length > 0)
                    {
                        mainGetSet.TotalWeight1 = rtv["total-weight"].InnerText;
                    }
                    if (rtv["tracking-num"].InnerText.Length > 0)
                    {
                        mainGetSet.TrackingNum1 = rtv["tracking-num"].InnerText;
                        lineGetSet.TrackingNum1 = rtv["tracking-num"].InnerText;
                    }
                    if (rtv["misc-charges-general"].InnerText.Length > 0)
                    {
                        mainGetSet.MiscChargesGeneral1 = rtv["misc-charges-general"].InnerText;
                    }
                    if (rtv["log-line-num"].InnerText.Length > 0)
                    {
                        mainGetSet.LogLineNum1 = rtv["log-line-num"].InnerText;
                    }
                    if (rtv["comment"].InnerText.Length > 0)
                    {
                        mainGetSet.Comment1 = rtv["comment"].InnerText;
                    }
                    if (rtv["merch-cost"].InnerText.Length > 0)
                    {
                        mainGetSet.MerchCost1 = rtv["merch-cost"].InnerText;
                    }
                    if (rtv["discount"].InnerText.Length > 0)
                    {
                        mainGetSet.Discount1 = rtv["discount"].InnerText;
                    }
                    if (rtv["misc-charges"].InnerText.Length > 0)
                    {
                        mainGetSet.MiscCharges1 = rtv["misc-charges"].InnerText;
                    }
                    if (rtv["freight-allowance"].InnerText.Length > 0)
                    {
                        mainGetSet.FreightAllowance1 = rtv["freight-allowance"].InnerText;
                    }
                    if (rtv["inbound-freight"].InnerText.Length > 0)
                    {
                        mainGetSet.InboundFreight1 = rtv["inbound-freight"].InnerText;
                    }
                    if (rtv["outbound-freight"].InnerText.Length > 0)
                    {
                        mainGetSet.OutboundFreight1 = rtv["outbound-freight"].InnerText;
                    }
                    if (rtv["ECommerceFrt"].InnerText.Length > 0)
                    {
                        mainGetSet.ECommerceFrt1 = rtv["ECommerceFrt"].InnerText;
                    }
                    if (rtv["total-amount"].InnerText.Length > 0)
                    {
                        mainGetSet.TotalAmount1 = rtv["total-amount"].InnerText;
                    }
                    if (rtv["gst-tax"].InnerText.Length > 0)
                    {
                        mainGetSet.GstTax1 = rtv["gst-tax"].InnerText;
                    }
                    if (rtv["qst-tax"].InnerText.Length > 0)
                    {
                        mainGetSet.QstTax1 = rtv["qst-tax"].InnerText;
                    }
                    if (rtv["hst-tax"].InnerText.Length > 0)
                    {
                        mainGetSet.HstTax1 = rtv["hst-tax"].InnerText;

                        ReturnsDB.AddReturnsToDB(mainGetSet);
                    }



                    XmlNodeList itemNodes = rtv.SelectNodes("item");
                    foreach (XmlNode item in itemNodes)
                    {

                        //MessageBox.Show(item["item-desc1"].InnerText);
                        if (item["quantity-returned"].InnerText.Length > 0)
                        {
                            lineGetSet.QuantityReturned1 = item["quantity-returned"].InnerText;
                        }
                        if (item["item-num"].InnerText.Length > 0)
                        {
                            lineGetSet.ItemNum1 = item["item-num"].InnerText;
                        }
                        if (item["item-desc1"].InnerText.Length > 0)
                        {
                            lineGetSet.ItemDesc11 = item["item-desc1"].InnerText;
                        }
                        if (item["net-cost"].InnerText.Length > 0)
                        {
                            lineGetSet.NetCost1 = item["net-cost"].InnerText;
                        }
                        if (item["extended-cost"].InnerText.Length > 0)
                        {
                            lineGetSet.ExtendedCost1 = item["extended-cost"].InnerText;
                            LinesDB.AddLinesToDB(lineGetSet);
                        }


                    }

                }


                string fileSplit = file.Split('\\').Last();

                File.Move(file, @"C:\_Data\CostcoXML\Archive\" + fileSplit);

            }

        }
        catch (Exception exc)
        {
            Console.WriteLine("There was an exception in the main program: " + exc);
        }

        Console.WriteLine("Finished writing from server.");
        Console.WriteLine(" Number of files written to our database (file in folder): " + counter);
        //Console.ReadKey();
        pause myPause = new pause();

        Console.WriteLine(DateTime.Now + " - Entering " + 10 + " Seconds. Pause");
        System.Threading.Thread.Sleep(10000);
        int timeOut = 10000;
        myPause.Pause(timeOut);
        // MessageBox.Show(timeOut.ToString());
        Console.WriteLine(DateTime.Now + " - Exiting Pause - Execute\r\n");

    }
}
}