基本事件处理程序问题

发布于 2024-10-03 08:40:51 字数 1544 浏览 5 评论 0原文

好吧,现在我有一个包含 3 个字符串的对象,以及 setter 和 getter。现在我有两个问题 -

首先,我是 C# 新手,有什么方法可以优化以下方法并使它们更高效吗?

    void getSearchResults(object sender, RoutedEventArgs e)
    {
        string baseURL = "http://api.search.live.net/xml.aspx?Appid=<MyAPPID>&query=%22";
        string companyName = ((TaxiCompany)sender).CoName;
        string formatAndKey = "%22&sources=web";
        WebClient c = new WebClient();
        c.DownloadStringAsync(new Uri(baseURL + companyName + formatAndKey));
        c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(findTotalResults);
    }


    //Parses search XML result to find number of results
    void findTotalResults(object sender, DownloadStringCompletedEventArgs e)
    {
        lock (this)
        {
            string s = e.Result;
            XmlReader reader = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(s)));
            String results = "";
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name.Equals("web:Total"))
                    {
                        results = reader.ReadInnerXml();
                        break;
                    }

                }
            }
        }
    }

其次,我正在初始化一个对象 - 新出租车公司(字符串名称、字符串电话、字符串结果)。我有姓名和号码,我需要调用上述两个函数来获取 noOfResults ,以便我可以初始化该对象。但是,我似乎遇到了事件处理程序的一系列问题。

我主要是一名网络开发人员,所以这里可能缺少一些非常基本的东西。我有一种感觉,设置 bing 方法以将字符串返回给构造函数可能是最简单的,但不太确定如何操作。

Ok right now I have an object containing 3 strings, along with setters and getters. Now I have two questions -

First, I'm new to C# is there any way to optimize the following methods and make them more efficient?

    void getSearchResults(object sender, RoutedEventArgs e)
    {
        string baseURL = "http://api.search.live.net/xml.aspx?Appid=<MyAPPID>&query=%22";
        string companyName = ((TaxiCompany)sender).CoName;
        string formatAndKey = "%22&sources=web";
        WebClient c = new WebClient();
        c.DownloadStringAsync(new Uri(baseURL + companyName + formatAndKey));
        c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(findTotalResults);
    }


    //Parses search XML result to find number of results
    void findTotalResults(object sender, DownloadStringCompletedEventArgs e)
    {
        lock (this)
        {
            string s = e.Result;
            XmlReader reader = XmlReader.Create(new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(s)));
            String results = "";
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name.Equals("web:Total"))
                    {
                        results = reader.ReadInnerXml();
                        break;
                    }

                }
            }
        }
    }

Second, I'm initializing an object - new Taxi Company (String name, String Phone, String Results). I've got name and number and I need to call the above two functions to get noOfResults so that I can initialize the object. However, I seem to run into a bunch of issues with the event handlers.

I've primarily been a web dev, so there might be something really basic I'm missing here. I have a feeling setting up the bing methods to return a string back to the constructor might be th easiest, but not quite sure how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2024-10-10 08:40:51

首先,你不需要主页上的锁。然后,我想说您的 XmlReader 块应该替换为名为 XDocument,这将允许您通过一行优雅的代码访问 XML 文档:

XDocument doc = XDocument.Parse(e.Result);

一旦您拥有该文档,您可以检查它是否包含特定的 XNode

First of all, you don't need the lock on the main page. Then, I would say that your XmlReader block should be replaced with the LINQ-to-XML variation called XDocument, that will allow you to access the XML document with a single, elegant line:

XDocument doc = XDocument.Parse(e.Result);

Once you have the document, you can check whether it contains a specific XNode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文