我正在尝试将 XML 反序列化为对象,但我遇到了一种情况。任何人都可以在这里帮助我。
XML:
<?xml version="1.0" ?>
<Level>
<Warp_Blocks>
<Warp_Block>
<Block row="7" col="7" />
<Block row="2" col="7" />
</Warp_Block>
<Warp_Block>
<Block row="4" col="4" />
<Block row="3" col="7" />
</Warp_Block>
</Warp_Blocks>
</Level>
代码:
[XmlRoot("Level")]
public class LData
{
[XmlArray("Warp_Blocks")]
[XmlArrayItem("Warp_Block",typeof(WarpBlock),IsNullable = false)]
public List<WarpBlock> WarpBlocks;
}
public class LBlock
{
[XmlAttribute("row")]
public int row;
[XmlAttribute("col")]
public int col;
}
public class WarpBlock
{
[XmlArray("Warp_Block")]
[XmlArrayItem("Block",typeof(LBlock),IsNullable= false)]
public List<LBlock> WarpBlocks;
public WarpBlock()
{
WarpBlocks = new List<LBlock>();
}
}
我能够反序列化到一个级别,即我得到一个 Item 对象列表,但单个 Item 对象不包含 Block 对象列表。我在这里做错了什么?
最佳答案
将您的 LData 类更改为此:
[XmlRoot("Level")]
public class LData
{
[XmlElement("Warp_Blocks")]
public List<WarpBlock> WarpBlocks;
}
编辑:
我不知道为什么它不读取您的第二个 Warp_Block。我认为唯一可能的原因可能是您正在做与您在问题中发布的内容不同的事情。这是完整的示例:
[XmlRoot("Level")]
public class LData
{
[XmlElement("Warp_Blocks")]
public List<WarpBlock> WarpBlocks;
}
public class LBlock
{
[XmlAttribute("row")]
public int row;
[XmlAttribute("col")]
public int col;
}
public class WarpBlock
{
[XmlArray("Warp_Block")]
[XmlArrayItem("Block", typeof(LBlock), IsNullable = false)]
public List<LBlock> WarpBlocks;
public WarpBlock()
{
WarpBlocks = new List<LBlock>();
}
}
public class Program
{
public static void Main()
{
string test =
"<?xml version=\"1.0\" ?>" +
"<Level>" +
" <Warp_Blocks>" +
" <Warp_Block>" +
" <Block row=\"7\" col=\"7\" />" +
" <Block row=\"2\" col=\"7\" />" +
" </Warp_Block>" +
" <Warp_Block>" +
" <Block row=\"4\" col=\"4\" />" +
" <Block row=\"3\" col=\"7\" />" +
" </Warp_Block>" +
" </Warp_Blocks>" +
"</Level>";
byte[] byteArray = Encoding.ASCII.GetBytes(test);
MemoryStream stream = new MemoryStream(byteArray);
XmlSerializer s = new XmlSerializer(typeof (LData));
LData data = (LData) s.Deserialize(stream);
foreach (var a in data.WarpBlocks)
foreach (var b in a.WarpBlocks)
Console.WriteLine(b.row + ", " + b.col);
Console.ReadKey();
}
}
它正确输出:
7, 7
2, 7
4, 4
3, 7
关于c# - 反序列化 List<ArrayList> 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5016558/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll