如何使用XML到LINQ访问命名空间内的XML

时间:2013-06-16 08:54:41

标签: .net xml linq linq-to-xml

我有以下XML:

   <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://x.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>
    <updated />
    <link rel="self" title="TestContents" href="TestContents" />

    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit"  />
        <content type="application/xml">
            <m:properties>
                <d:Title>ssq</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit"  />
        <content type="application/xml">
            <m:properties>
                <d:Title>yy</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit" />
        <content type="application/xml">
            <m:properties>
                <d:Title>xx</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
</feed>

我想访问<d:Title>yy</d:Title>

的值

以下是Darin向我建议的代码:

    static void Main(string[] args) {

        XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
        using (var reader = XmlReader.Create(@"c:\data\contents2.xml"))
        {
            var feed = SyndicationFeed.Load(reader);
            foreach (var item in feed.Items)
            {
                var content = item.Content as XmlSyndicationContent;
                if (content != null)
                {
                    var value = content.ReadContent<XElement>();
                    var text = value.Element(d + "Title");
                    Console.WriteLine(text.Value);
                }
            }
            Console.ReadLine();
        }
    }

但是当我运行它时它只是给我一个错误

Error in line 5 position 3. An error was encountered when parsing a DateTime value in the XML.

1 个答案:

答案 0 :(得分:3)

看起来您正在尝试解析联合供稿。我会建议你使用适当的工具 - &gt; SyndicationFeed

XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
using (var reader = XmlReader.Create("test.xml"))
{
    var feed = SyndicationFeed.Load(reader);
    foreach (var item in feed.Items)
    {
        var content = item.Content as XmlSyndicationContent;
        if (content != null)
        {
            var value = content.ReadContent<XElement>();
            var text = value.Element(d + "Title");
            Console.WriteLine(text.Value);
        }
    }
}

注意您需要使用适当的XNamespace来选择自定义<d:Title>元素。

如果您不想使用SyndicationFeed类,可以使用Linq to XML手动解析XML:

XNamespace a = "http://www.w3.org/2005/Atom";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XElement feed = XElement.Load("test.xml");
var titles =
    from entry in feed.Descendants(a + "entry")
    let content = entry.Element(a + "content")
    let properties = content.Element(m + "properties")
    let title = properties.Element(d + "Title")
    select title;

foreach (var title in titles)
{
    Console.WriteLine(title.Value);            
}