我有XML喜欢
<rss>
<channel>
<item>
<category domain="category" nicename="change"><![CDATA[Changing Lives]]></category>
<category domain="category" nicename="events"><![CDATA[Events]]></category>
<category domain="category" nicename="leadership"><![CDATA[Leadership]]></category>
<category domain="category" nicename="spiritual-transformation"><![CDATA[Spiritual Transformation]]></category>
</item>
<item></item>
<item></item>
</channel>
</rss>
我正在尝试阅读类别innertext(改变生活,事件,领导力)...使用foreach条件..但是对于每个循环我只能改变生活..这里是我的代码
protected void btnImportPost_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
string strPath = Server.MapPath("~/App_Data/willowcreekassociationblog.wordpress.xml");
doc.Load(strPath);
//Get Channel Node
XmlNode channelNode = doc.SelectSingleNode("rss/channel");
if (channelNode != null)
{
DateTime temp;
//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"))
{
//some code here
foreach (XmlNode categoryNode in itemNode.SelectNodes("category"))
{
//CMS.SiteProvider.CategoryInfo GetCate = null;
string CategoryName = itemNode.SelectSingleNode("category").InnerText;
Response.Write(@"<script language='javascript'>alert('root Document:" + CategoryName + "');</script>");
}
}
}
}
答案 0 :(得分:1)
只需使用
string CategoryName = categoryNode.InnerText;
答案 1 :(得分:0)
原因是你不是每次循环都写出itemNode的InnerText,而是使用SelectSingleNode来拔出第一个匹配。
尝试将内循环更改为:
foreach (XmlNode categoryNode in itemNode.SelectNodes("category"))
{
//CMS.SiteProvider.CategoryInfo GetCate = null;
//string CategoryName = itemNode.SelectSingleNode("category").InnerText; // incorrect
string CategoryName = categoryNode.InnerText;
Console.Write(CategoryName);
}