使用 C# 4.0 动态 XML 导航和阅读?
.NET 4.0 中是否有支持 DLR 的 XML 导航和阅读类?例如,假设我有这个 XML:
<foo>
<bar>foobar is here</bar>
<bar>foobar is also here</bar>
<baz>foobar is not here</bar>
</foo>
是否有一种简单的方法可以像这样浏览此 XML:
var doc = SomeDlrEnabledParser.Parse (xmlString);
foreach (var node in doc.foo.bar)
{
if (node == "foobar is here")
DoSomething();
else
DoSomethingElse();
}
我可以看到上述方法出现问题的很多原因,包括命名空间、属性与元素、区分集合与单个元素 特征
但我处理的大部分 XML 都非常简单且只读,我愿意接受合理的默认行为,以换取避免“括号和引号汤”的工作 在 4.0 之前的世界中使用简单的 XML。
例如,“点”运算符可以在子元素名称之前检查属性名称。或者非集合操作将自动应用于第一个元素(就像 jQuery 那样)。
.NET 4.0 Framework 类库是否包含类似的内容?如果没有,有什么好的开源项目或支持 DLR 的 XML 库示例的建议吗?
Is there a DLR-enabled XML navigation and reading class available in .NET 4.0? For example, imagine I have this XML:
<foo>
<bar>foobar is here</bar>
<bar>foobar is also here</bar>
<baz>foobar is not here</bar>
</foo>
Is there an easy way to navigate through this XML like this:
var doc = SomeDlrEnabledParser.Parse (xmlString);
foreach (var node in doc.foo.bar)
{
if (node == "foobar is here")
DoSomething();
else
DoSomethingElse();
}
I can see lots of reasons why the approach above would be problematic, including namespaces, attributes vs. elements, differentiating collections vs. single elements, encoded XML vs text, etc.
But much of the XML I deal with is very simple and read-only, and I'd be willing to accept sensible default behaviors in exchange for avoiding the "parentheses and quote soup" that's characteristic of working with simple XML in a pre-4.0 world.
For example, the "dot" operator could check attribute names before subelement names. Or non-collection operations would be automatically applied to the first element (like jQuery does).
Does the .NET 4.0 Framework Class Library contain anything like this? if not, any recommendations for a good open-source project or sample of a DLR-enabled XML library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了一些测试实现:
和示例:
但是,似乎这个和其他一些代码之间的接口不是很好。
例如,如果我们想对节点使用 Linq,我们必须首先显式转换为
IEnumerable
...否则我们的Where
将被解释为元素名称。您可以直接在动态类型上实现
Where
,但是您必须像这样限定所有 lambda:也就是说,如果您只是像这样从 XML 树中提取值,我认为它可以正常工作。
I did a little test implementation:
And example:
However, it seems that the interface between this and some other code is not so nice.
For example, if we wanted to use Linq against the nodes we must first explicitly cast to an
IEnumerable
... otherwise ourWhere
will be interpreted as an element name.You can implement
Where
directly on the dynamic type, but you must then qualify all lambdas like this:That said, if you're just extracting values from an XML tree like this, I think it works okay.