Scenario:.I have 30 tips in xml file, now I want to display it on home page by day number.
For example, today is the 12th then tip number 12 will be displayed开发者_如何学运维 on the home page and after that 13 and so on. After 30 tips it will start from tip 1.
I have tried following code
public class TipReader
{
private const string TIP_XML_PATH = @"C:\temp\tips.xml";
public static int GetTipNumber()
{
int tipNo = 1;
if (DateTime.Now.Day >0)
{
if (DateTime.Now.Day == 31)
{
tipNo = 1;
}
else if (DateTime.Now.Day == 28)
{
tipNo = DateTime.Now.Day + 1;
}
else
{
tipNo = DateTime.Now.Day;
}
}
return tipNo;
}
public IEnumerable<Tip> GetTipByID(string ID)
{
var result = from tip in XDocument.Load(TIP_XML_PATH).Descendants("Tip")
where (string)tip.Element("ID")==ID
select new Tip
{
ImageUrl = (string)tip.Element("ImageUrl"),
Title = (string)tip.Element("Title").Value,
Desciption = (string)tip.Element("Description"),
};
return result;
}
}
Now, I am facing issue on GetTipNumber. How would I handle date greater than 30 and less than 30. If you have better solution for this please suggest me. Thanks in advance
What is the harm in doing it in this fashion
public static int GetTipNumber()
{
int tipNo = 1;
if (DateTime.Now.Day > 0 && DateTime.Now.Day <= 30)
{
tipNo = DateTime.Now.Day;
}
else
{
tipNo = 1; //
}
return tipNo;
}
int tipNo = DateTime.Now.Day > 30 ? 1 : DateTime.Now.Day;
Another option is to display a random tip each time you view the page:
public static int GetTipNumber()
{
Random rand = new Random();
return rand.Next(1, 30);
}
精彩评论