我可能担心错误的优化,但我有一个唠叨的想法,它一遍又一遍地解析 xml 树,也许我在某个地方读过它。不记得了。
无论如何,这就是我正在做的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Net;
namespace LinqTestingGrounds
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("http://www.dreamincode.net/forums/xml.php?showuser=335389"));
Console.ReadLine();
}
static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
XDocument xml = XDocument.Parse(e.Result);
User user = new User();
user.ID = xml.Element("ipb").Element("profile").Element("id").Value;
user.Name = xml.Element("ipb").Element("profile").Element("name").Value;
user.Rating = xml.Element("ipb").Element("profile").Element("rating").Value;
user.Photo = xml.Element("ipb").Element("profile").Element("photo").Value;
user.Reputation = xml.Element("ipb").Element("profile").Element("reputation").Value;
user.Group = xml.Element("ipb").Element("profile").Element("group").Element("span").Value;
user.Posts = xml.Element("ipb").Element("profile").Element("posts").Value;
user.PostsPerDay = xml.Element("ipb").Element("profile").Element("postsperday").Value;
user.JoinDate = xml.Element("ipb").Element("profile").Element("joined").Value;
user.ProfileViews = xml.Element("ipb").Element("profile").Element("views").Value;
user.LastActive = xml.Element("ipb").Element("profile").Element("lastactive").Value;
user.Location = xml.Element("ipb").Element("profile").Element("location").Value;
user.Title = xml.Element("ipb").Element("profile").Element("title").Value;
user.Age = xml.Element("ipb").Element("profile").Element("age").Value;
user.Birthday= xml.Element("ipb").Element("profile").Element("birthday").Value;
user.Gender = xml.Element("ipb").Element("profile").Element("gender").Element("gender").Element("value").Value;
Console.WriteLine(user.ID);
Console.WriteLine(user.Name);
Console.WriteLine(user.Rating);
Console.WriteLine(user.Photo);
Console.WriteLine(user.Reputation);
Console.WriteLine(user.Group);
Console.WriteLine(user.Posts);
Console.WriteLine(user.PostsPerDay);
Console.WriteLine(user.JoinDate);
Console.WriteLine(user.ProfileViews);
Console.WriteLine(user.LastActive);
Console.WriteLine(user.Location);
Console.WriteLine(user.Title);
Console.WriteLine(user.Age);
Console.WriteLine(user.Birthday);
Console.WriteLine(user.Gender);
//Console.WriteLine(xml);
}
}
}
这是 Good Enough™ 还是有更快的方法来解析我需要的东西?
附言。我在 DownloadStringCompleted 事件中执行大部分操作,我不应该这样做吗?第一次用这种方法。谢谢!
最佳答案
不知道效率如何,但为了可读性,使用 profile 变量而不是一遍又一遍地遍历整个事情:
User user = new User();
var profile = xml.Element("ipb").Element("profile");
user.ID = profile.Element("id").Value;
关于c# - 这是解析 XML 的低效方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4015971/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer