我有一串类型string xml = @"<recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><daily dayFrequency=""1"" /></repeat><windowEnd>2012-10-31T10:00:00Z</windowEnd></rule></recurrence>";
我想在这里读取dayFrequency
值,即1
,是否有一种方法可以直接在标签daily
下读取dayFrequency
,并且同样有许多这样的标签,例如 A =" 1",B =" KIND"等。所以我想直接阅读分配给变量的值。
请帮助。
我使用的以下代码读取重复标签
string xml = @"<recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><daily dayFrequency=""1"" /></repeat><windowEnd>2012-10-31T10:00:00Z</windowEnd></rule></recurrence>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
// this would select all title elements
XmlNodeList titles = xmlDoc.GetElementsByTagName("repeat");
XDocument xmlDoc = XDocument.Parse(xml);
var val = xmlDoc.Descendants("daily")
.Attributes("dayFrequency")
.FirstOrDefault();
这里的Val将是:
val = {dayFrequency="1"}
val.Value
将为您提供1
XElement.Parse(xml).Descendants("daily")
.Single()
.Attribute("dayFrequency")
.Value;
XDocument xdoc = XDocument.Parse(@"<recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><daily dayFrequency=""1"" /></repeat><windowEnd>2012-10-31T10:00:00Z</windowEnd></rule></recurrence>");
string result = xdoc
.Descendants("recurrence")
.Descendants("rule")
.Descendants("repeat")
.Descendants("daily")
.Attributes("dayFrequency")
.First()
.Value;
您应该使用getAttribute()。
有关更多信息,请参见:http://msdn.microsoft.com/en-us/library/acwfyhc7.aspx
var nodes = xmlDoc.SelectNodes(path);
foreach (XmlNode childrenNode in nodes)
{
HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("//repeat").Value);
}