C# 在C中读取XML字符串#
我有一个类型为C# 在C中读取XML字符串#,c#,asp.net,xml,readxml,C#,Asp.net,Xml,Readxml,我有一个类型为stringxml=@“mo2012-10-31T10:00:00Z”的字符串 我想读取dayFrequency值,它是1这里,有没有一种方法可以直接读取标签dayFrequency下的dayFrequency,同样有很多这样的标签,比如a=“1”、b=“King”等。因此,我想直接读取分配给变量的值 请帮忙 下面是我用来读取repeat标签的代码 string xml = @"<recurrence><rule><firstDayOfWeek>
stringxml=@“mo2012-10-31T10:00:00Z”的字符串代码>
我想读取dayFrequency
值,它是1
这里,有没有一种方法可以直接读取标签dayFrequency
下的dayFrequency
,同样有很多这样的标签,比如a=“1”、b=“King”等。因此,我想直接读取分配给变量的值
请帮忙
下面是我用来读取repeat标签的代码
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");
stringxml=@“mo2012-10-31T10:00:00Z”;
XmlDocument xmlDoc=新的XmlDocument();
LoadXml(xml);
//这将选择所有标题元素
XmlNodeList titles=xmlDoc.GetElementsByTagName(“重复”);
您应该使用getattribute()
有关更多信息,请参阅:
下面是:
val = {dayFrequency="1"}
val.Value
将为您提供1
XDocument xdoc=XDocument.Parse(@“mo2012-10-31T10:00:00Z”);
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;
字符串结果=xdoc
.后代(“复发”)
.后代(“规则”)
.后代(“重复”)
.后代(“每日”)
.属性(“日频率”)
.First()
价值
什么与您的代码不兼容?dayFrequency=“”1”错误。“需要编码为”如果我没记错的话。为什么不使用XLinq?
XDocument xmlDoc = XDocument.Parse(xml);
var val = xmlDoc.Descendants("daily")
.Attributes("dayFrequency")
.FirstOrDefault();
val = {dayFrequency="1"}
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;