理解使用linq解析xml
我这里只是有一些代码来澄清我对 linq xml 解析的疑问。我有以下内容:
...
{
XDocument xmlDoc = XDocument.Load(@"C:\Build.xml");
var abc = from example in xmlDoc.Descendants("target")
select (string)target.Attribute("if");
...
foreach(string example in abc)
{
...
}
...
}
我可以问一下 select (string)target.Attribute("if") 行中的内容是否属实
我从 xml 文件中的“if”值中选择一个字符串,如下面的 xml 文件所示:
<xml>
<target if="thevalue">
</target>
</xml>
然后我有这一行: foreach(abc 中的字符串示例)
是否真的对于“if”属性的“值”的每个选定字符串,我正在 foreach 循环中执行某些操作。
I just have some codes here to clarify my doubts on linq xml parsing. I have the following:
...
{
XDocument xmlDoc = XDocument.Load(@"C:\Build.xml");
var abc = from example in xmlDoc.Descendants("target")
select (string)target.Attribute("if");
...
foreach(string example in abc)
{
...
}
...
}
Can i ask that if it is true that in the line select (string)target.Attribute("if")
i am selecting a string from the xml file from the value of "if" as shown in the xml file below:
<xml>
<target if="thevalue">
</target>
</xml>
then i have this line:foreach(string example in abc)
Is it true that for every selected string of the "value" of the "if" attribute, i am doing something in the foreach loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是正确的。 foreach 循环中的变量“example”将包含“if”属性的值。
在控制台应用程序中尝试上述代码,您将在控制台窗口中看到这些值。或者您可以在不同类型的应用程序中使用 Debug.WriteLine(example)
这是一个更好的 linq 查询表达式,
或者将您的 linq 查询表达式更改为
EDIT
为了帮助解决调试器问题:
我将鼠标移到 abc 上,看到调试器弹出信息窗口,如下图所示
第一个图像显示“结果视图”有那些绿色箭头。您需要单击这些箭头才能看到结果(如它们旁边的消息所示),
然后第二张图片向您显示结果
Yes, this is correct. The variable "example" in your foreach loop will contain the value of the "if" attribute.
try the above code in a console application and you'll see the values in the console window. Or you could use Debug.WriteLine(example) in a different type of application
This is a better linq query expression however
or change your linq query expression to
EDIT
To help with your issue with the Debugger:
I've moved my mouse over abc and I see the debugger pop up the information window as shown in the image below
This first images show the "Results View" has those green arrows. You need to click on those arrows before you can see the result (as the message next to them says)
The second image then shows you the results