我无法使用文件中的LINQ to XML访问XML数据

时间:2013-06-16 07:39:51

标签: c#

我在名为contents.xml的文件中有以下XML,该文件位于我的C盘中的数据目录中:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
  <feed xml:base="http://datacenter1.table.core.windows.net/" 
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
    xmlns="http://www.w3.org/2005/Atom">
    <title type="text">TestContents</title>
    <entry>
        <title type="text" />
    <author>
        <name />
        </author>
        <link rel="edit" title="TestContents" />
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey>0100000</d:PartitionKey>
                <d:Text>xx</d:Text>
            </m:properties>
        </content>
    </entry>
    <entry>
        <title type="text" />
        <updated />

我需要获取<d:Text>的值,所以我创建了这个控制台应用程序:

namespace ConsoleApplication1
{
    class Program
    {
       static void Main(string[] args)
       {
           XDocument xmlDoc = XDocument.Load(@"c:\data\contents.xml");
           var q = from c in xmlDoc.Descendants("entry")
              select (string)c.Element("link") ;
           Console.WriteLine(q);
       }

    }
}

但我有两个问题。首先控制台显示但在我看到输出之前消失。第二,如果我在调试器中查看q,它说“枚举不返回结果”。

我可以用什么来获得我真正需要的最好的方法是多少<d:Text>的价值?

1 个答案:

答案 0 :(得分:1)

第一个问题:在Console.WriteLine(q)行之后,您可以编写Console.ReadLine()。因此,在您按“Enter”键之前,将显示结果。

第二个问题:如果你想要列表中的所有值,那么你可以做q.ToList()