c#如何读如xml里的值?
大家好,以下xml什么读取出count和mcount的值,
<root><data><159578108090313><count>201</count><mcount>4</mcount></159578108090313><52892026821632><count>317</count><mcount>5</mcount></52892026821632></data></root>
data后面的那个是数字是变化的,
我用
var infoNodes = this.Xml.SelectNodes("data");
foreach (XmlNode n in infoNodes)
{
recounts.Add(new ReCount(n));
}
protected override void Parse()
{
this.Count = Util.GetXmlNodeValue<long>(this.Xml.SelectSingleNode("count"));
this.MCount = Util.GetXmlNodeValue<long>(this.Xml.SelectSingleNode("mcount"));
}
读取不行,以现以下错误
System.Xml.XmlException: 名称不能以“1”字符(十六进制值 0x31)开头。 行 1,位置 14。
------解决方案--------------------
C# code
string str = "<root><data><159578108090313><count>201</count><mcount>4</mcount></159578108090313><52892026821632><count>317</count><mcount>5</mcount></52892026821632></data></root>";
string reg = @"(\d+)><count>(\d+)</count><mcount>(\d+)";
var ms = Regex.Matches(str, reg);
foreach (Match m in ms)
{
Response.Write(string.Format("第一个={0},count={1},mcount={2}<br>", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value));
}