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>");
  • 相关阅读:
    29-赫夫曼树
    28-线索化二叉树
    27-顺序存储二叉树
    26-二叉树的遍历查找和删除
    25-二叉树的概念
    24-逻辑结构分析
    23-哈希表
    22-查找算法
    21-堆排序
    Mui-列表/table-view
  • 原文地址:https://www.cnblogs.com/SkySoot/p/2584950.html
Copyright © 2011-2022 走看看