I know its a lot code… sorry for that….my list class is like this..
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; }
}
my c# code is like this
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;
}
i have xml like enter link description here
when I am trying to read for each item in channel with xmlnode and its working fine. and each item has multiple comments which trying to achieve by using foreach inside foreach. But wp:comment foreach item its not working. What am i doing wrong? I did some google but no luck.
Thanks.
This is resetting your
itemenumeration to the beginning, which would cause every item to have identical comments.EDIT:
I believe this is the easiest way to fix the problem I mentioned. By searching below the already selected node, you avoid the repetition. Note that
cNodeis no longer required.