当前位置: 代码迷 >> C# >> 关于取出期数和数字
  详细解决方案

关于取出期数和数字

热度:55   发布时间:2016-05-05 03:57:31.0
求助关于取出期数和数字
<?xml version="1.0" encoding="utf-8"?><xml rows="5" code="cqssc" info="免费接口随机延迟3-5分钟如需实时接口请访问opencai.net或加QQ:9564384(请注明彩票API)"><row expect="20150502036" opencode="9,9,4,6,5" opentime="2015-05-02 12:00:40"/><row expect="20150502035" opencode="9,1,1,8,6" opentime="2015-05-02 11:50:40"/><row expect="20150502034" opencode="3,7,1,9,5" opentime="2015-05-02 11:40:40"/><row expect="20150502033" opencode="0,5,5,4,1" opentime="2015-05-02 11:30:40"/><row expect="20150502032" opencode="3,7,7,3,8" opentime="2015-05-02 11:20:40"/></xml>

如何取出期数 和开奖结果呢
例如上面的代码取出的结果是
20150502036       9,9,4,6,5
20150502035       9,1,1,8,6
20150502034       3,7,1,9,5
20150502033       0,5,5,4,1
20150502032       3,7,7,3,8 
------解决思路----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = @"<?xml version=""1.0"" encoding=""utf-8""?><xml rows=""5"" code=""cqssc"" info=""免费接口随机延迟3-5分钟如需实时接口请访问opencai.net或加QQ:9564384(请注明彩票API)""><row expect=""20150502036"" opencode=""9,9,4,6,5"" opentime=""2015-05-02 12:00:40""/><row expect=""20150502035"" opencode=""9,1,1,8,6"" opentime=""2015-05-02 11:50:40""/><row expect=""20150502034"" opencode=""3,7,1,9,5"" opentime=""2015-05-02 11:40:40""/><row expect=""20150502033"" opencode=""0,5,5,4,1"" opentime=""2015-05-02 11:30:40""/><row expect=""20150502032"" opencode=""3,7,7,3,8"" opentime=""2015-05-02 11:20:40""/></xml>";
            var query = Regex.Matches(s, @"expect\=\""(\d{11})\""\sopencode\=\""(\d,\d,\d,\d,\d)\""").Cast<Match>().Select(x => new { a = x.Groups[1], b = x.Groups[2] });
            foreach (var item in query)
                Console.WriteLine(string.Concat(item.a, "\t", item.b));
        }
    }
}


20150502036     9,9,4,6,5
20150502035     9,1,1,8,6
20150502034     3,7,1,9,5
20150502033     0,5,5,4,1
20150502032     3,7,7,3,8
Press any key to continue . . .
------解决思路----------------------
如此标准的xml,上面给了正则的,这边就给xml的吧
static void ReadXMLRow()
        {
            string xml = @"<?xml version=""1.0"" encoding=""utf-8""?><xml rows=""5"" code=""cqssc"" info=""免费接口随机延迟3-5分钟如需实时接口请访问opencai.net或加QQ:9564384(请注明彩票API)""><row expect=""20150502036"" opencode=""9,9,4,6,5"" opentime=""2015-05-02 12:00:40""/><row expect=""20150502035"" opencode=""9,1,1,8,6"" opentime=""2015-05-02 11:50:40""/><row expect=""20150502034"" opencode=""3,7,1,9,5"" opentime=""2015-05-02 11:40:40""/><row expect=""20150502033"" opencode=""0,5,5,4,1"" opentime=""2015-05-02 11:30:40""/><row expect=""20150502032"" opencode=""3,7,7,3,8"" opentime=""2015-05-02 11:20:40""/></xml>";
            XElement root = XElement.Parse(xml);
            foreach(var row in root.Elements("row"))
            {
                Console.WriteLine("expect:{0} opencode:{1}", row.Attribute("expect").Value, row.Attribute("opencode").Value);
            }
        }

------解决思路----------------------
能够认识到它是一个 xml,这是是基于语法的,适用于产品级的要求。比如说

< row expect="20150502035" opentime="2015-05-02 11:50:40"        opencode    ="9,1,1,8,6">
    <others>
        <row expect="20150502033" opencode="0,5,5,4,1" opentime="2015-05-02 11:30:40" />
        ......
     </others>
</row>
<room expect="20150502034" opencode="3,7,1,9,5" opentime="2015-05-02 11:40:40"/>
<row id="8892342" expect="20150502033" opencode="0,5,5,4,1" opentime="2015-05-02 11:30:40" />

即使有嵌套文档、大小写、闭合方式、空格字符个数、属性位置等等区别,也能区分出这里只有2个row符合查询规则。
  相关解决方案