【问题标题】:Read xml with repeat elements using xdocument使用 xdocument 读取带有重复元素的 xml
【发布时间】:2012-07-26 04:18:24
【问题描述】:

我需要 xml 文件重复的项目。在“项目”内,我有标题、发布日期、描述、dc:creator 和 repeatwp:comment 等字段......请参阅下面的 xml 文件..

<channel>
    <item>
        <title>What Messed With My Head: Summit 2011</title>
        <link>http://www.wcablog.com/2011/08/what-messed-with-my-head-summit-2011/</link>
        <pubDate>Fri, 26 Aug 2011 09:10:04 +0000</pubDate>
        <dc:creator>willowcreekassociation</dc:creator>
        <guid isPermaLink="false">http://www.wcablog.com/?p=1706</guid>
        <description></description>
        <content:encoded>
            <![CDATA[text here]]>
        </content:encoded>
        <wp:comment>
            <wp:comment_id>1016</wp:comment_id>
            <wp:comment_author><![CDATA[]]></wp:comment_author>
            <wp:comment_author_email>thelmabowlen@gmail.com</wp:comment_author_email>
            <wp:comment_author_url></wp:comment_author_url>
            <wp:comment_author_IP></wp:comment_author_IP>
            <wp:comment_date>2011-08-26 20:13:00</wp:comment_date>
            <wp:comment_content><![CDATA[some text ]]></wp:comment_content>
        </wp:comment>
        <wp:comment>
            <wp:comment_id>1016</wp:comment_id>
            <wp:comment_author><![CDATA[]]></wp:comment_author>
            <wp:comment_author_email>thelmabowlen@gmail.com</wp:comment_author_email>
            <wp:comment_author_url></wp:comment_author_url>
            <wp:comment_author_IP></wp:comment_author_IP>
            <wp:comment_date>2011-08-26 20:13:00</wp:comment_date>
            <wp:comment_content><![CDATA[some text ]]></wp:comment_content>
        </wp:comment>
    </item>
    <item>
        <title>What Messed With My Head: Summit 2011</title>
        <link>http://www.wcablog.com/2011/08/what-messed-with-my-head-summit-2011/</link>
        <pubDate>Fri, 26 Aug 2011 09:10:04 +0000</pubDate>
        <dc:creator>willowcreekassociation</dc:creator>
        <guid isPermaLink="false">http://www.wcablog.com/?p=1706</guid>
        <description></description>
        <content:encoded>
            <![CDATA[text here]]>
        </content:encoded>
    </item>
    <item>
        <title>What Messed With My Head: Summit 2011</title>
        <link>http://www.wcablog.com/2011/08/what-messed-with-my-head-summit-2011/</link>
        <pubDate>Fri, 26 Aug 2011 09:10:04 +0000</pubDate>
        <dc:creator>willowcreekassociation</dc:creator>
        <guid isPermaLink="false">http://www.wcablog.com/?p=1706</guid>
        <description></description>
        <content:encoded>
            <![CDATA[text here]]>
        </content:encoded>
        <wp:comment></wp:comment>
        <wp:comment></wp:comment>
    </item>
</channel>

我正在使用以下代码来读取 xml..

XDocument xDoc = XDocument.Load("willowcreekassociationblog.wordpress.xml");

        var list = xDoc.Descendants("Occurrence")
        .Select(o => new List.XMLList
        {
            title = (string)o.Element("title"),
            URL = (string)o.Element("link"),
            Descr = (string)o.Element("Description"),
            StartDate = (DateTime)o.Element("pubdate"),
        })
        .ToList();

但我不知道如何使用上面的代码阅读 wp:comment...谁能帮助我如何做到这一点??

【问题讨论】:

    标签: c# linq linq-to-xml


    【解决方案1】:

    你没有说你想对数据做什么,这使得这个问题很难回答。您还没有显示 wp 命名空间别名的定义位置,但是嘿...

    您希望在您的Select 电话中 像这样:

    Comments = o.Elements(wp + "comment")
                .Select(comment => Comment.FromXElement(comment))
                .ToList();
    

    我发现在可以执行从XElement 转换的域对象中提供静态方法很有帮助 - 它使事情更清晰。

    FromXElement 将类似于:

    public static Comment FromXElement(XElement x)
    {
        return new Comment((int) x.Element(wp + "comment_id"),
                           (string) x.Element(wp + "author"),
                           ...);
    }
    

    【讨论】:

    • 我只需要读取数据并将其分配给列表..并将数据保存到数据库中..
    • @Srav:好吧,我假设你已经有一个类型来表示评论,就像你有一个类型来表示项目一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多