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

Download a XML file through a WebBrowser Control in C#

我有一个 Windows 表单,我给了一个 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 文件一个不同的名称,或者甚至是一种在下载时捕获它以便我可以解析的方法数据?谢谢。

我通过取消 Windows 表单并完全依赖使用宏记录器、C# 控制台应用程序和任务计划程序的自动化解决了这个问题。首先,我创建了宏记录器来登录网站并导航到相应的页面,选择要下载的最新日期,然后将 XML 文件下载到正确的文件夹中。然后我创建了一个 C# 应用程序,它从文件位置提取文件(我不得不将文件作为字符串提取,因为 XML 文件太大,任何其他方法都会截断数据并使文件无效 XML) 并解析 XML 文件,将其加载到 SQL 服务器数据库中,然后将文件存档。所有这些都是通过我的服务器任务调度程序完成的。这是来自主 C# class 的代码,这是我发现的解析大型 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");

    }
}
}