封装嵌套 XML
我正在尝试找到一种将 XML 封装到 C# 类中的优雅方法。环顾四周,我在某处发现了一个 Java 示例(不要问我在哪里),它激发了一个想法,但我不确定它在 C# 中是否可能,或者我是否以正确的方式进行处理。
我当前使用的不优雅的方法是使用一系列嵌套的 switch 语句迭代 XPathNodeIterator/XPathNavigator 节点,这些语句指示找到适当的节点后要执行的操作,并列出所有未处理的节点。我尝试的更优雅的方法涉及使用辅助类来完成大部分迭代,并且基本上采用节点名称以及遇到该节点时要采取的操作。该类目前看起来像这样(请注意,这是理论上的,请参阅下文了解原因):
class XmlNodeIterator
{
public XmlNodeIterator(XPathNodeIterator node, Dictionary<string, Action> children)
{
foreach (XPathNavigator childNode in node.Current.SelectChildren(XPathNodeType.Element))
{
Action child = null;
try
{
child = children[childNode.LocalName];
}
catch (KeyNotFoundException e)
{
// Log the warning in some fashion
}
if (child != null)
child.Invoke();
}
}
}
在我进行概念验证测试之前,我发现了这个概念的问题。 :) 如何访问委托函数中的内部 childNode(例如,childNode.Value 或 childNode.GetAttribute)并将其分配给外部变量或属性?
总的来说,我对代表来说还是个新手,所以我不知道这是否可能。如果是的话,那就太好了!如果没有,我可以尝试其他优雅的解决方案吗?或者我应该坚持使用嵌套开关?
I'm trying to find an elegant way of encapsulating XML into a C# class. Looking around, I found a Java example somewhere (don't ask me where at this point) that sparked an idea, but I'm not sure if it's even possible in C# or if I'm going about it the right way.
The inelegant method I'm using currently is to iterate through XPathNodeIterator/XPathNavigator nodes with a whole series of nested switch statements that indicate what to do once the appropriate node is found, and also list any nodes that were unhandled. The more elegant method I'm trying involves using a helper class to do the bulk of the iteration and basically takes a node name and what Action to take when that node is encountered. That class looks like this currently (note that this is theoretical, see below for why):
class XmlNodeIterator
{
public XmlNodeIterator(XPathNodeIterator node, Dictionary<string, Action> children)
{
foreach (XPathNavigator childNode in node.Current.SelectChildren(XPathNodeType.Element))
{
Action child = null;
try
{
child = children[childNode.LocalName];
}
catch (KeyNotFoundException e)
{
// Log the warning in some fashion
}
if (child != null)
child.Invoke();
}
}
}
Before I even got to proof-of-concept testing, I discovered a problem with the concept. :) How can I access the inner childNode (e.g., childNode.Value or childNode.GetAttribute) in the delegate function and assign it to an outer variable or property?
I'm still really new to delegates in general, so I have no idea if this is even possible. If it is, great! If not, are there any other elegant solutions I can try? Or should I just stick with nested switches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑使用
Action
而不是Action
。它的Invoke方法可以带参数调用。在您的情况下:或者,如果您想分配任何外部值,您可以使用
Func
,如下所示:顺便说一句,如第二个示例所示,可以省略直接 Invoke 调用。我们可以简单地将其称为方法
Consider using
Action<T>
instead ofAction
. It's Invoke method can be called with parameter. In your case:Or if you want to assign any outer values you can use
Func<T1,T2>
like that:Btw, as shown in 2nd example, direct Invoke call can be omited. We can simply call it as a method