我知道它有很多代码......对不起......我的列表类是这样的......
public class XMLList
{
public string Title { get; set; }
[DataMember]
public string Link { get; set; }
[DataMember]
public DateTime pubDate { get; set; }
[DataMember]
public string dcCreator { get; set; }
[DataMember]
public string GUID { get; set; }
[DataMember]
public Int32 wpPostId { get; set; }
[DataMember]
public string wpStatus { get; set; }
[DataMember]
public Int32 wpMenuOrd { get; set; }
[DataMember]
public string Category { get; set; }
[DataMember]
public List<Comment> Comments { get; set; }
}
public class Comment
{
[DataMember]
public Int32 wpCmtId { get; set; }
[DataMember]
public string wpCmtAuthor { get; set; }
[DataMember]
public string wpCmtAuthorEmail { get; set; }
[DataMember]
public string wpCmtAuthorURL { get; set; }
[DataMember]
public Int64 wpCmtAuthorIP { get; set; }
[DataMember]
public DateTime wpCmtAuthorDate { get; set; }
}
我的 c# 代码是这样的
XmlDocument doc = new XmlDocument();
doc.Load(@"xml\willowcreekassociationblog.wordpress.xml");
//Get Channel Node
XmlNode channelNode = doc.SelectSingleNode("rss/channel");
if (channelNode != null)
{
//Add NameSpace
XmlNamespaceManager nameSpace = new XmlNamespaceManager(doc.NameTable);
nameSpace.AddNamespace("excerpt", "http://wordpress.org/export/1.2/excerpt/");
nameSpace.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
nameSpace.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
nameSpace.AddNamespace("wfw", "http://wellformedweb.org/CommentAPI/");
nameSpace.AddNamespace("wp", "http://wordpress.org/export/1.2/");
//Parse each item
foreach (XmlNode itemNode in channelNode.SelectNodes("item"))
{
objrssItem.Add(rssItem);
rssItem.GUID = itemNode.SelectSingleNode("guid").InnerText;
rssItem.Title = itemNode.SelectSingleNode("title").InnerText;
rssItem.dcCreator = itemNode.SelectSingleNode("dc:creator", nameSpace).InnerText;
rssItem.Link = itemNode.SelectSingleNode("link").InnerText;
rssItem.pubDate = DateTime.Parse(itemNode.SelectSingleNode("pubDate").InnerText);
rssItem.ContentEncoded = itemNode.SelectSingleNode("content:encoded", nameSpace).InnerText;
XmlNode cNode = doc.SelectSingleNode("rss/channel/item");
foreach (XmlNode commentNode in cNode.SelectNodes("wp:comment", nameSpace))
{
//rssItem.Comments = Comments
rsscomment.wpCmtId = Convert.ToInt32(commentNode.SelectSingleNode("wp:comment_id", nameSpace).InnerText);
rsscomment.wpCmtAuthor = commentNode.SelectSingleNode("wp:comment_author", nameSpace).InnerText;
rsscomment.wpCmtContent = commentNode.SelectSingleNode("wp:comment_content", nameSpace).InnerText;
}
}
oXMLListResult.listOfXMLResult = objrssItem;
}
当我尝试使用 xmlnode 读取通道中的每个项目时,它工作正常。每个项目都有多个注释,试图通过在 foreach 中使用 foreach 来实现。但是 wp:comment foreach 项目它不起作用。我究竟做错了什么?我做了一些谷歌但没有运气。
谢谢。