基本事件处理程序问题
好吧,现在我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,你不需要主页上的锁。然后,我想说您的 XmlReader 块应该替换为名为 XDocument,这将允许您通过一行优雅的代码访问 XML 文档:
一旦您拥有该文档,您可以检查它是否包含特定的 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:
Once you have the document, you can check whether it contains a specific XNode.