求助关于取出期数和数字

.Net技术 码拜 7年前 (2015-05-10) 714次浏览 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 

45分
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 . . .

楼上已经给出

建议楼主自己学习 正则表达式

45分
如此标准的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都可以
10分
能够认识到它是一个 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符合查询规则。

把它当作一个简单的字符串来匹配个别字符,这通常上是出于“临时解答一下眼前的问题”的目的。当在负责开发一个系统的数据处理程序时,那时的场景就不会这么随意了,你的团队里的有经验的程序员或者你的技术经理们就会在意你是使用语法解析方式还是拼字符串方式了。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求助关于取出期数和数字
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!