我创建了一个XML字符串并循环获取值。但它没有进入foreach循环。但在我的其他代码中,相同的循环代码正在运行。 我的代码是:
XML字符串:
string str = CD.SRList.Replace("&", "&");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(str);
XmlNode SuggestionListNode = xmlDoc.SelectSingleNode("/SuggestedReadings/Suggestion");
foreach (XmlNode node in SuggestionListNode)
{
COURSESUGGESTEDREADING CSR = new COURSESUGGESTEDREADING();
var s = db.COURSESUGGESTEDREADINGS.OrderByDescending(o => o.SRID);
CSR.SRID = (s == null ? 0 : s.FirstOrDefault().SRID) + 1;
CSR.COURSEID = LibId;
CSR.TEXT = node.Attributes.GetNamedItem("Text").Value;
CSR.LINK = node.Attributes.GetNamedItem("Link").Value; ;
CSR.SUGBY = node.Attributes.GetNamedItem("SuggBy").Value; ;
CSR.ACTIVEFLAG = "Y";
CSR.CREATEDBY = CD.CreatedBy;
CSR.CREATEDDATE = DateTime.Now;
db.COURSESUGGESTEDREADINGS.Add(CSR);
}
代码是:
string strRegXPattern = @"(?<date>(?<day>\d{1,2})-(?<month>\d{1,2})-(?<year>(?:\d{4}|\d{2}))\s(?<time>(?<hour>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})(?<milli>\.?\d{0,3}))):\s(?<textEntry>[\s\S]*?)(?=(?:\d{1,2}-\d{1,2}-(?:\d{4}|\d{2})\s\d{2}:\d{2}:\d{2}\.?\d{0,3})|$)";
答案 0 :(得分:4)
您应该使用SelectNodes
,而不是SelectSingleNode
,因为您试图从XML文档中获取多行。
使用此:
XmlNodeList SuggestionListNode = xmlDoc.SelectNodes("//Suggestion");
foreach (XmlNode node in SuggestionListNode)
{
}
答案 1 :(得分:0)
你可以试试这个。
XDocument xdoc = XDocument.Load(“data.xml”); var xmlData =来自xdoc.Descendants中的lv1(“建议”) 选择新{ Text = lv1.Attribute(“Text”)。Value, Link = lv1.Attribute(“Link”)。Value, SuggBy = lv1.Attribute(“SuggBy”)。值 }; foreach(xmlData中的var项){ //你的逻辑在这里 }