将谷歌旅行模式放入xml网址

时间:2016-12-24 00:10:46

标签: c# html asp.net xml

我想要做的是当选择旅行模式时,它会将值放入xml网址并提供我需要的信息。

此图片是用于选择google api旅行模式的按钮,图片下方是代码。

<div class="btn-group" role="group" >
        <button runat="server" type="button" class="btn btn-default" id="btnDrive"><span aria-hidden="true" class="fa fa-car"></span></button>
        <button runat="server" type="button" class="btn btn-default" id="btnWalk"><span aria-hidden="true" class="fa fa-wheelchair-alt"></span></button>
        <button runat="server" type="button" class="btn btn-default" id="btnBike"><span aria-hidden="true" class="fa fa-bicycle"></span></button>
        <button runat="server" type="button" class="btn btn-default" value="transit" id="btnTransit"><span aria-hidden="true" class="fa fa-train"></span></button>
    </div>

以下是xml信息的网址。 &#34;模式=&#34;旁边我想把任何模式都选中。

String GoogleURL = "https://maps.googleapis.com/maps/api/distancematrix/xml?units=imperial&mode=" +  + "&origins=" + txtA.Text + "&destinations=" + txtB.Text + "&key=AIzaSyBApHnE1nXT3lsyS_CZqtXr8j1p0BhpQZA";

1 个答案:

答案 0 :(得分:0)

试试这个

           string xml =
                "<div class=\"btn-group\" role=\"group\" >" +
                    "<button runat=\"server\" type=\"button\" class=\"btn btn-default\" id=\"btnDrive\"><span aria-hidden=\"true\" class=\"fa fa-car\"></span></button>" +
                    "<button runat=\"server\" type=\"button\" class=\"btn btn-default\" id=\"btnWalk\"><span aria-hidden=\"true\" class=\"fa fa-wheelchair-alt\"></span></button>" +
                    "<button runat=\"server\" type=\"button\" class=\"btn btn-default\" id=\"btnBike\"><span aria-hidden=\"true\" class=\"fa fa-bicycle\"></span></button>" +
                    "<button runat=\"server\" type=\"button\" class=\"btn btn-default\" value=\"transit\" id=\"btnTransit\"><span aria-hidden=\"true\" class=\"fa fa-train\"></span></button>" +
                "</div>";

            XElement button = XElement.Parse(xml);
            string selection = button.Elements("button").Where(x => (string)x.Attribute("value") != null).Select(x => (string)x.Attribute("value")).FirstOrDefault();

            string units = "imperial";
            string mode = string.Format("%22%20+%20{0}%20+%20%22", selection);
            string origin = "%22%20+%20txtA.Text%20+%20%22";
            string destination = "%22%20+%20txtB.Text%20+%20%22";
            string key = "AIzaSyBApHnE1nXT3lsyS_CZqtXr8j1p0BhpQZA";
            string url = string.Format("https://maps.googleapis.com/maps/api/distancematrix/xml?units={0}&mode={1}&origins={2}&destinations={3}&key={4}",
                units, mode, origin, destination, key);