zoukankan      html  css  js  c++  java
  • 根据定制的 XML 文件进行随机抽取节

    此类库中的两个类可以达成这一的一些效果:每次打开网页展现不同的标语、问候语,根据语录内容随机出题,随机显示新闻等等。当然XML格式的定制或者根据不同的XML文件适当修改类字段还是必要的。

    using System;
    using System.Xml;
     
    namespace Quotations
    {
        public class QuotationManager
        {
            private XmlDocument quoteDoc;
            private int quoteCount;
     
            public QuotationManager(string fileName)
            {
                quoteDoc = new XmlDocument();
                quoteDoc.Load(fileName);
                quoteCount = quoteDoc.DocumentElement.ChildNodes.Count;
            }
     
            public Quotation GetRandomQuote()
            {
                int i;
                Random x = new Random();
                i = x.Next(quoteCount - 1);
                return new Quotation(quoteDoc.DocumentElement.ChildNodes[i]);
            }
        }
    }
    using System;
    using System.Xml;
     
    namespace Quotations
    {
        public class Quotation
        {
            public string Source { get; set; }
            public string Date { get; set; }
            public string QuotationText { get; set; }
     
            public Quotation(XmlNode quoteNode)
            {
                if (quoteNode.SelectSingleNode("source") != null)
                {
                    Source = quoteNode.SelectSingleNode("source").InnerText;
                }
     
                if (quoteNode.SelectSingleNode("date")!=null)
                {
                    Date = quoteNode.SelectSingleNode("date").InnerText;
                }
     
                QuotationText = quoteNode.FirstChild.InnerText;
            }
        }
    }

    这样调用:

    string filePath = Server.MapPath("./quotations.xml");
    Quotations.QuotationManager manager = new Quotations.QuotationManager(filePath);
    Quotations.Quotation quotation = manager.GetRandomQuote();
    Response.Write("<b>" + quotation.Source + "</b>(<i>" + quotation.Date + "</i>)");
    Response.Write("<blockquote>" + quotation.QuotationText + "</blockquote>");
  • 相关阅读:
    golang中os/user包用法
    golang中os包用法
    与table有关的布局
    jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version
    canvas里调用getImageData的报security的问题
    CSS样式覆盖规则
    windows7文件夹怎样默认图片大图显示?
    Jboss,Tomcat 远程调试配置
    IE(IE6/IE7/IE8)支持HTML5标签
    JS 继承(类式 与 原型式)
  • 原文地址:https://www.cnblogs.com/SkySoot/p/2584950.html
Copyright © 2011-2022 走看看