通过LINQ读取XML [英] Readng XML via LINQ

查看:125
本文介绍了通过LINQ读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于最近的建议...


并且在未能制作一个简单的程序来使用代码并查看它的功能之后...我来了问题,并且寻求帮助...


控制台应用

展开 | 选择 | Wrap < span class =codeDivider> | 行号

解决方案

< blockquote>只是猜测路径dk.xml不会导致文件。

我认为默认搜索是检查与可执行文件相同的文件夹。

也许用文件测试一个已知的有效位置,如 ; C:\\dk.xml"


感谢您的快速回复。


我尝试将XML文件放入C: /。相同的结果。

我尝试使用Console.Write代替。

我尝试使用data.ToString()。


所有"人才与QUOT;我的XML文件中的标签没有结束标记(虽然语法符合w3)。所以我试着读一个带有结束标签的标签......仍然没有去...


只是想我会试着看看它是否做了什么。虽然它似乎没有奏效。 = \


我最终想写这篇文章,但我没有时间。我会在这里做一个快速浓缩版本供您开始使用。我真的很喜欢LINQ并希望每个人都开始使用它= D


首先,如果你还没有确定添加对System.Xml.Linq的引用,并确保它在你的使用陈述中。 (我看到你已经有了,但是我把它放在这里是为了别人的利益)。


以下代码的第一部分获取所有后代节点XML文档中的根元素,然后遍历它们并打印它们的值。我使用了标准的长符号。


第二部分显示了查找xml中特定字符串的查询,即名为body的第一个后代节点的值。我在这个例子中使用了速记和lambda表达式。


这是一个可以帮助你入门的样本。
C#Program

展开 | 选择 | Wrap | 行号


Due to a recent suggestion...

And after failing to make a simple program to play with the code and see what it does... I come bearing questions, and seeking help...

Console App

Expand|Select|Wrap|Line Numbers

解决方案

Just guessing that the path "dk.xml" does not lead to a file.
I think the default search is to check in the same folder as the executable.
Perhaps testing with the file a known valid location like "C:\\dk.xml"


Thanks for the quick response.

I tried putting the XML file in the C:/ . Same result.
I tried using Console.Write instead.
I tried using data.ToString().

All the "Talent" tags in my XML file with don''t have a closing tag (The syntax is w3 compliant though). So I tried to read a tag with a closing tag... Still no go...

Just figured I would try and see if it did anything. Though it didn''t seem to work. =\


I''ve been meaning to do a writeup of this eventually, but I haven''t had time. I''ll do a quick condensed version here for you to get started. I really love LINQ and want everyone to start using it =D

First, make sure to add a reference to System.Xml.Linq if you haven''t, and make sure it''s in your using statements. (I see that you already have, but I''m putting this here for other people''s benefit).

The first part of the following code gets all the descendant nodes of the root element in the XML document, then loops through them and prints their values. I used the standard long notation for this.

The second part shows a query to find a specific string in the xml, namely the value of the first descendant node called "body". I used shorthand and a lambda expression for this example.

Here''s a sample that can get you started.
C# Program

Expand|Select|Wrap|Line Numbers


这篇关于通过LINQ读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆