C# 使用xdocument读取包含重复元素的xml
我需要用xml文件重复这个项目。在“项目”中,我有诸如标题、发布日期、描述、dc:creator和带有repeatwp:comment的字段。。。请参见下面的xml文件C# 使用xdocument读取包含重复元素的xml,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我需要用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-mes
<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>
但是我不知道如何阅读我上面代码的wp:comment…有人能帮我怎么做吗???你还没有说你想对数据做什么,这使得这个问题很难回答。您还没有显示
wp
名称空间别名的定义位置,但是
您想要这样的内容,请在中选择调用:
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:我假设您已经有一个类型来表示注释,就像您有一个类型来表示项目一样。
public static Comment FromXElement(XElement x)
{
return new Comment((int) x.Element(wp + "comment_id"),
(string) x.Element(wp + "author"),
...);
}