更新:已解决 - 见下文!
反序列化包含网络节点的 xml 文件时,在处理子元素时似乎出现了问题。
我有以下父(根)类:NetworkCollection
[Serializable, XmlRoot("networks"), XmlType("networks")]
public class NetworkCollection : CollectionBase, IBindingList
{
// ...
}
与子类:网络
[Serializable, XmlRoot("network"), XmlType("network")]
public class Network
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
// ...
[XmlElement("nodes")]
public NodeCollection Nodes { get; set; }
[XmlElement("to_add")]
public NodeCollection NodesToAdd { get; set; }
[XmlElement("to_remove")]
public NodeCollection NodesToRemove { get; set; }
[XmlElement("available")]
public NodeCollection NodesAvailable { get; set; }
}
NodeCollection 和 Node 相同
[Serializable, XmlRoot("nodes"), XmlType("nodes")]
public class NodeCollection : CollectionBase, IBindingList
{
// ...
}
[Serializable, XmlRoot("node"), XmlType("node")]
public class Node
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlElement("mac_address")]
public string MacAddress { get; set; }
}
有不同的节点类型,像这样
[Serializable, XmlRoot("node_type_a"), XmlType("node_type_a")]
public class NodeTypeA : Node
{
// ...
}
[Serializable, XmlRoot("node_type_b"), XmlType("node_type_b")]
public class NodeTypeB : Node
{
// ...
}
[Serializable, XmlRoot("node_type_c"), XmlType("node_type_c")]
public class NodeTypeC : Node
{
// ...
}
XML 看起来像这样
<?xml version="1.0" encoding="ISO-8859-1"?>
<networks>
<network id="bcb468cac8a34052bf5a5bad8f22d0d7">
<name>Network Name</name>
<nodes>
<node_type_a id="0ac4774277a941228b27d8cfd7ef202e">
<mac_address>AABBCCDDEEFF</mac_address>
</node_type_b>
<node_type_a id="4ca5abb4bb8742c3b321250c20592542">
<mac_address>BBCCDDEEFFAA</mac_address>
</node_type_a>
<node_type_b id="bde044c9aa1948978ba1bc980f00c36b">
<mac_address>CCDDEEFFAABB</mac_address>
</node_type_b>
<node_type_c id="d3f2cb7bd92e4dc89930eeb3cdaeb680">
<mac_address>DDEEFFAABBCC</mac_address>
</node_type_c>
<node_type_c id="039d140b5cc54e06b915b91fb5d8acdf">
<mac_address>EEFFAABBCCDD</mac_address>
</node_type_c>
</nodes>
<to_add/>
<to_remove/>
<available>
<node>
<mac_address>FFAABBCCDDEE</mac_address>
</node>
<node>
<mac_address>001122334455</mac_address>
</node>
<node>
<mac_address>112233445566</mac_address>
</node>
<node>
<mac_address>223344556677</mac_address>
</node>
</available>
</network>
</networks>
发生的情况是每个 NodeCollection 都包含一个元素,所以
Nodes (1),
NodesToAdd (1),
NodesToRemove (1) and
NodesAvailable (1)
都包含一个 Node 对象,其属性 (MacAddress) 设置为 null;
应该是
Nodes (5),
NodesToAdd (0),
NodesToRemove (0) and
NodesAvailable (4)
反序列化 NetworkCollection 和 Network 工作正常。它只是关于 NodeCollection 属性。
请注意使用的 XmlRoot 和 XmlType 属性。 可用节点的“Id”可能为空/缺失。
如何处理? 问候
好的,我已经通过替换解决了问题
[XmlElement("nodes")]
public NodeCollection Nodes { get; set; }
[XmlElement("to_add")]
public NodeCollection NodesToAdd { get; set; }
[XmlElement("to_remove")]
public NodeCollection NodesToRemove { get; set; }
[XmlElement("available")]
public NodeCollection NodesAvailable { get; set; }
与
[XmlArray("nodes")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection Nodes { get; set; }
[XmlArray("to_add")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection NodesToAdd { get; set; }
[XmlArray("to_remove")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection NodesToRemove { get; set; }
[XmlArray("available")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection NodesAvailable { get; set; }
问候。
最佳答案
好的,我已经通过替换解决了问题
[XmlElement("nodes")]
public NodeCollection Nodes { get; set; }
[XmlElement("to_add")]
public NodeCollection NodesToAdd { get; set; }
[XmlElement("to_remove")]
public NodeCollection NodesToRemove { get; set; }
[XmlElement("available")]
public NodeCollection NodesAvailable { get; set; }
与
[XmlArray("nodes")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection Nodes { get; set; }
[XmlArray("to_add")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection NodesToAdd { get; set; }
[XmlArray("to_remove")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection NodesToRemove { get; set; }
[XmlArray("available")]
[XmlArrayItem(ElementName = "node", Type = typeof(Node))]
[XmlArrayItem(ElementName = "node_type_a", Type = typeof(NodeTypeA))]
[XmlArrayItem(ElementName = "node_type_b", Type = typeof(NodeTypeB))]
[XmlArrayItem(ElementName = "node_type_c", Type = typeof(NodeTypeC))]
public NodeCollection NodesAvailable { get; set; }
问候。
关于c# - 将 XML 反序列化为不同元素上的相同类型的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17085424/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最
查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.