Tuesday, November 19, 2013

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.XPath;
using System.Xml.Linq;
using System.Xml;
using System.IO;

 public class ExamModel
    {
        readonly string xmlPath = string.Empty;

        public ExamModel(string path)
        {
            xmlPath = path;
        }

        public string ReadXML(string league)
        {
            string str = "<table>";
            XPathDocument doc = new XPathDocument(xmlPath);
            XPathNavigator nav = doc.CreateNavigator();
            XPathExpression exp = (league == "All") ? nav.Compile(@"/Leagues/League") : nav.Compile(@"/Leagues/League[@name='" + league + "']");
            XPathNodeIterator iterator = nav.Select(exp);

            while (iterator.MoveNext())
            {
                XPathNavigator selector = iterator.Current.SelectSingleNode("@name");
                string strLeague = selector == null ? string.Empty : selector.Value.ToString();

                XPathNodeIterator iterator1 = iterator.Current.Select("Race");
                while (iterator1.MoveNext())
                {
                    string strRaceName = string.Empty;
                    string strTime = string.Empty;
                    strRaceName += GetNode(iterator1.Current, "@name").Trim();
                    strTime += GetNode(iterator1.Current, "Time").Trim();
                    str += "<tr>" + strRaceName + strTime + "</tr>";
                }
            }
            return str;
        }

        private string GetNode(XPathNavigator path, string xPath)
        {
            string res = string.Empty;
            int i = 0;
            XPathNodeIterator iterator = path.Select(xPath);
            while (iterator.MoveNext())
            {
                i++;
                res += "<td>" + iterator.Current.Value.ToString() + "</td>";
            }
            return res;
        }
    }

No comments:

Post a Comment