使用c#获取ajax加载内容的最佳方法是什么?

时间:2015-07-26 04:35:00

标签: ajax xpath loaded

我需要使用HtmlAgility或Xpath获取外部HTML页面的ajax加载内容。但是一些内容是由Ajax从外部资源加载的。

获取这些内容的最佳方式是什么?

谢谢

1 个答案:

答案 0 :(得分:0)

**这是我在博客上发布的一个示例脚本,它具有JSON repsonse类型的url端点的一般方法和格式。 **         http://chadcollins.com/create-table-from-json-data/

    // set the content type so the browser(s) will buffer the output correctly.
    Response.ContentType = "application/json";

    // create new data access object
    DataAccessObject NewDao = Settings.Connection1DataAccessObject;

    string column_Name = "";
    string innerItems = "";
    string innerProductTypes = "";
    string sqlRowString = "";
    string _pid = Request.QueryString["pid"];
    string innerColumnKeyNames = "";
    string innerColumnKeyValues = "";
    string productTypeFooter = "";

    // check the querystring for the filter key id for the category table Product_Type
    if (_pid != null && _pid.Length != 0) {

    // get columns from the table dynamically. This was the start of an attempt to get the key(column) and row(values) from a standard select below. 
    // ive taken the hardcoded approach for now in setting the product type    
    SqlCommand SelectCol = new SqlCommand("SELECT TOP 1 *  FROM customtable_Rates_Items",NewDao);
    DataColumnCollection newColumn = SelectCol.Execute().Tables[0].Columns;

            for(int x = 0; x < newColumn.Count; x++){
                    column_Name = newColumn[x].ToString();
                    sqlRowString += column_Name +",";
            }     

    // remove the last , in sqlRowString
    sqlRowString = sqlRowString.TrimEnd(',');

        // Get Items
        SqlCommand Select = new SqlCommand("SELECT "+sqlRowString+" FROM customtable_Rates_Items WHERE Rates_Product_Type_ID="+ 
        NewDao.ToSql(_pid.ToString()+" order by Order_Precedence",FieldType.Integer),NewDao);

        DataRowCollection newDr = Select.Execute().Tables[0].Rows;

            // Get Product Type - Name
            SqlCommand Select2 = new SqlCommand("SELECT ItemID,Product_Type_Name,Product_Type_Footer, Product_Type_Header, Column_Key_Names, Column_Display_Values FROM customtable_Rates_Product_Type WHERE ItemID="+ 
            NewDao.ToSql(_pid.ToString(),FieldType.Integer),NewDao);

            DataRowCollection newDr2 = Select2.Execute().Tables[0].Rows;
                    innerProductTypes =  "\n    {\"Product_Type_Name\" : \"" + newDr2[0]["Product_Type_Name"] + "\" } \n        ";
                    innerColumnKeyNames = ""+newDr2[0]["Column_Key_Names"];
                    innerColumnKeyValues = ""+newDr2[0]["Column_Display_Values"];
                    productTypeFooter = ""+newDr2[0]["Product_Type_Footer"];

        for(int y = 0; y < newDr.Count; y++){
            // add key name and value output elements here for json output.
            innerItems += "\n    {";
            innerItems +="\"ItemID\" : "+ "\"" + newDr[y]["ItemID"] + "\" , \"Daily_Balance\" : "  + "\"" + newDr[y]["Daily_Balance"] + "\" , \"Dividend_Rate\" : "  + "\"" + newDr[y]["Dividend_Rate"] + "\" , \"Annual_Percentage_Yield\" : "  + "\"" + newDr[y]["Annual_Percentage_Yield"] + "\" , \"Period_of_Investment_Dividend\" : "  + "\"" + newDr[y]["Period_of_Investment_Dividend"] + "\" , \"Minimum_Investment\" : "  + "\"" + newDr[y]["Minimum_Investment"] + "\" , \"When_Dividend_Paid\" : "  + "\"" + newDr[y]["When_Dividend_Paid"] + "\" , \"Maximum_Of\" : "  + "\"" + newDr[y]["Maximum_Of"] + "\" , \"Interest_Rate\" : "  + "\"" + newDr[y]["Interest_Rate"] + "\" , \"Points\" : "  + "\"" + newDr[y]["Points"] + "\" , \"Annual_Percentage_Rate\" : "  + "\"" + newDr[y]["Annual_Percentage_Rate"] + "\" , \"Est_Monthly_Payments\" : "  + "\"" + newDr[y]["Est_Monthly_Payments"] + "\" , \"Max_Term\" : "  + "\"" + newDr[y]["Max_Term"] + "\" , \"Max_Amount\" : "  + "\"" + newDr[y]["Max_Amount"] + "\" , \"Max_LTV_Percent\" : "  + "\"" + newDr[y]["Max_LTV_Percent"] + "\" , \"Loan_Type\" : "  + "\"" + newDr[y]["Loan_Type"] + "\" , \"Card_Type\" : "  + "\"" + newDr[y]["Card_Type"] + "\" } \n        ,";
        }     
         innerItems = innerItems.TrimEnd(',');      //this can be done better
         //Output directly the json dataset record.
         Response.Write("[\r");     
         Response.Write("{\r");
         Response.Write("\"pid\": \"1\",\n");
         Response.Write("\"footer\": \""+productTypeFooter+"\",\n");
         Response.Write("\"column_key_names\":[\r" + innerColumnKeyNames + "\r],\n\r");
         Response.Write("\"column_key_values\":[\r" + innerColumnKeyValues + "\r],\n\r");
         Response.Write("\"product_type\":[\r" + innerProductTypes + "\r],\n\r");
         Response.Write("\"items\":[\r" + innerItems + "\r]");
         Response.Write("\r}\r]\r");
      }

使用参数调用时会返回类似

的内容
[
          {
            "pid": "1",
            "footer": "*Annual Percentage Yield",
            "column_key_names": [
              "Daily_Balance",
              "Dividend_Rate",
              "Annual_Percentage_Yield",
              "Max_LTV_Percent"
            ],
            "column_key_values": [
              "foo",
              "bar",
              "dude",
              "test1"
            ],
            "product_type": [
              {
                "Product_Type_Name": "Checking Account Rates"
              }
            ],
            "items": [
              {
                "ItemID": "1",
                "Daily_Balance": "$0.00 - $999.99",
                "Dividend_Rate": "0.00%",
                "Annual_Percentage_Yield": "0.00%",
                "Period_of_Investment_Dividend": "",
                "Minimum_Investment": "",
                "When_Dividend_Paid": "",
                "Maximum_Of": "",
                "Interest_Rate": "",
                "Points": "",
                "Annual_Percentage_Rate": "",
                "Est_Monthly_Payments": "",
                "Max_Term": "",
                "Max_Amount": "",
                "Max_LTV_Percent": "66 month term = 120% of factory invoice plus tax, license, GAP and extended warranty. No additional discounts may be applied. 75 and 84 month financing available at slightly higher interest rates. 48 month term does not qualify for relationship pricing discounts.",
                "Loan_Type": "",
                "Card_Type": ""
              },
              {
                "ItemID": "2",
                "Daily_Balance": "$1,000.00 - $9,999.99",
                "Dividend_Rate": "0.01%",
                "Annual_Percentage_Yield": "0.01%",
                "Period_of_Investment_Dividend": "",
                "Minimum_Investment": "",
                "When_Dividend_Paid": "",
                "Maximum_Of": "",
                "Interest_Rate": "Interest_Rate",
                "Points": "",
                "Annual_Percentage_Rate": "",
                "Est_Monthly_Payments": "",
                "Max_Term": "",
                "Max_Amount": "",
                "Max_LTV_Percent": "",
                "Loan_Type": "",
                "Card_Type": ""
              },
              {
                "ItemID": "3",
                "Daily_Balance": " $10,000.00 +",
                "Dividend_Rate": "0.02%",
                "Annual_Percentage_Yield": "0.02%",
                "Period_of_Investment_Dividend": "",
                "Minimum_Investment": "",
                "When_Dividend_Paid": "",
                "Maximum_Of": "",
                "Interest_Rate": "Interest_Rate",
                "Points": "",
                "Annual_Percentage_Rate": "",
                "Est_Monthly_Payments": "",
                "Max_Term": "",
                "Max_Amount": "",
                "Max_LTV_Percent": "",
                "Loan_Type": "",
                "Card_Type": ""
              },
              {
                "ItemID": "4",
                "Daily_Balance": "<strong>Ultra Rewards Rates<\/strong>",
                "Dividend_Rate": "",
                "Annual_Percentage_Yield": "",
                "Period_of_Investment_Dividend": "",
                "Minimum_Investment": "",
                "When_Dividend_Paid": "",
                "Maximum_Of": "",
                "Interest_Rate": "Interest_Rate",
                "Points": "",
                "Annual_Percentage_Rate": "",
                "Est_Monthly_Payments": "",
                "Max_Term": "",
                "Max_Amount": "",
                "Max_LTV_Percent": "",
                "Loan_Type": "",
                "Card_Type": ""
              },
              {
                "ItemID": "5",
                "Daily_Balance": "$0 - 25,000.00",
                "Dividend_Rate": "0.65%",
                "Annual_Percentage_Yield": "0.65%",
                "Period_of_Investment_Dividend": "",
                "Minimum_Investment": "",
                "When_Dividend_Paid": "",
                "Maximum_Of": "",
                "Interest_Rate": "Interest_Rate",
                "Points": "",
                "Annual_Percentage_Rate": "",
                "Est_Monthly_Payments": "",
                "Max_Term": "",
                "Max_Amount": "",
                "Max_LTV_Percent": "",
                "Loan_Type": "",
                "Card_Type": ""
              },
              {
                "ItemID": "6",
                "Daily_Balance": "$25,000.01 +",
                "Dividend_Rate": "0.02%",
                "Annual_Percentage_Yield": "0.02%",
                "Period_of_Investment_Dividend": "",
                "Minimum_Investment": "",
                "When_Dividend_Paid": "",
                "Maximum_Of": "",
                "Interest_Rate": "Interest_Rate",
                "Points": "",
                "Annual_Percentage_Rate": "",
                "Est_Monthly_Payments": "",
                "Max_Term": "",
                "Max_Amount": "",
                "Max_LTV_Percent": "",
                "Loan_Type": "",
                "Card_Type": ""
              }
            ]
          }
        ]

最后你会用Ajax /或jQuery查询这个脚本得到甚至加载