1

我有以下 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.
4

1 回答 1

3

看起来您正在尝试解析联合提要。我建议您为此使用适当的工具 -> 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);            
}
于 2013-06-16T09:06:02.487 回答