使用 linq 扩展方法选择 xml 中的元素
我对 linq 不太熟悉,想知道如何使用扩展方法(不使用查询表达式)根据应用程序名称在以下 xml 中选择应用程序
<applicationlist>
<application>
<name>test1</name>
<ele1>852</ele1
<ele2>http://localhost/test1</ele2>
</application>
<application>
<name>test2</name>
<ele1>456</ele1
<ele2>http://localhost/test2</ele2>
</application>
</applicationlist>
I am little new to linq and was wondering how i can select the application in the following xml based on the application name using Extension Methods (not using the query expression)
<applicationlist>
<application>
<name>test1</name>
<ele1>852</ele1
<ele2>http://localhost/test1</ele2>
</application>
<application>
<name>test2</name>
<ele1>456</ele1
<ele2>http://localhost/test2</ele2>
</application>
</applicationlist>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设“选择的 SQL 方式”是指“使用查询表达式”,让我们从查询表达式开始:
使用扩展方法,这只是:
我建议不要以这种方式进行不区分大小写的比较 - 它有文化问题。请使用类似这样的内容:(
或其他
StringComparison
选项之一 - 或StringComparer
的实例)。您可能还想修剪
appName
一次,而不是每次比较都修剪...Assuming that by "the SQL way of selecting" you mean "using a query expression", let's start off with your query expression:
With extension methods, this would just be:
I would recommend against making case-insensitive comparisons this way though - it has cultural problems. Use something like this instead:
(or one of the other
StringComparison
options - or an instance ofStringComparer
).You might also want to trim
appName
once rather than for every comparison...