选择随机节点并添加到标签

时间:2017-01-19 14:32:31

标签: c# xml

我是c#的新手,并尝试制作一个小随机数发生器。 我想在mystatus = 1中选择一个随机节点,并在标签中显示series_titleseries_image

Xml:

<?xml version="1.0" encoding="UTF-8"?>
<myanimelist>
 <myinfo>
  <user_id>5144371</user_id>
  <user_name>berefin</user_name>
  <user_watching>116</user_watching>
  <user_completed>100</user_completed>
  <user_onhold>3</user_onhold>
  <user_dropped>0</user_dropped>
  <user_plantowatch>52</user_plantowatch>
  <user_days_spent_watching>18.65</user_days_spent_watching>
</myinfo>
<anime>
  <series_title>Cowboy Bebop</series_title>
  <series_image>https://myanimelist.cdn- dena.com/images/anime/4/19644.jpg</series_image>
  <my_status>1</my_status>
</anime>
<anime>
  <series_title>Naruto</series_title>
  <series_image>https://myanimelist.cdn-dena.com/images/anime/13/17405.jpg</series_image>
  <my_status>1</my_status>
</anime>
<anime>
  <series_title>One Piece</series_title>      
  <series_image>https://myanimelist.cdn-dena.com/images/anime/6/73245.jpg</series_image>
  <my_status>2</my_status>
 </anime>

到目前为止的守则:

private void btnRandom_MouseClick(object sender, MouseEventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("https://myanimelist.net/malappinfo.php?u=berefin&status=all&type=anime");

        XmlNodeList list = doc.SelectNodes("/myanimelist/anime");
        //string content = doc.InnerXml;

        foreach (XmlNode node in list)
        {
            // Not sure what to do here
            Random random = new Random();

            string my_status = node["my_status"].InnerText;


            if (my_status == "1")
            {
                string series_title = node["series_title"].InnerText;
                string series_image = node["series_image"].InnerText;


            }
        }

    }

如何随机使用xml节点?

3 个答案:

答案 0 :(得分:1)

首先,您需要创建一个仅包含my_status为1的节点的新列表。您可以使用foreach循环填充此列表。

然后你会想要得到这个新列表的大小 - 在你的情况下它应该是2.然后你可以使用:

int index = random.Next(0, size_of_new_list);
XmlNode node = size_of_new_list[index];

答案 1 :(得分:1)

有两种方法,我将描述一个我更喜欢的方法。另一个将按照您将获得适合的元素数量的方式工作,然后在阅读期间您将找到具有您从Random获得的数字的元素。

像:

Random r = new Random();

//some code to get counted elements

int myOpt = r.Next( CountOfElements );

// some code to run through elemenets
if (myOpt == iterator)
{
    //get the details about Anime
}

填充(实现)到数组(列表)然后随机选择一个

这个是理想的,如果你想稍后对数组做一些工作,例如修改它或做任何工作。最好的方法是创建具有Your对象定义的类,创建此对象的数组然后填充它。最后的瘦是从数组中找到随机实体。

班级定义:

public class Anime
{
    public string series_title { get; set; }
    public string series_image { get; set; }
    public int my_status { get; set; }
}

填写清单:

private void btnRandom_MouseClick(object sender, MouseEventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load("https://myanimelist.net/malappinfo.php?u=berefin&status=all&type=anime");
    XmlNodeList list = doc.SelectNodes("/myanimelist/anime");
    var arr = new List<Anime>();

    foreach (XmlNode node in list)
    {
        arr.Add(new Anime()
        {
            series_title = node["series_title"].InnerText;
            series_image = node["series_image"].InnerText;
            my_status = Convert.ToInt32(node["my_status"].InnerText);
        } 
    }

    //here is all Your animes in list -> arr
}

最后你可以随机选择一个:

public Anime PickRandom(List<Anime> list)
{
    Random random = new Random();

    return list[random.Next(list.Count)];
}

答案 2 :(得分:1)

也许不适合初学者,但这有效:

你的开始:

XmlDocument doc = new XmlDocument();     
doc.Load("https://myanimelist.net/malappinfo.php?u=berefin&status=all&type=anime");
XmlNodeList list = doc.SelectNodes("/myanimelist/anime");

使用正确的my_status创建列表:

var okList = list.Cast<XmlNode>().Where(xn => xn["my_status"].InnerText == "1");

从Oklist中获取0到最多项目的随机数:

var numOk = okList.Count( );
Random random = new Random();     
var numChosen = random.Next(numOk);

在此位置获取节点:

var node = okList.Skip(numChosen).First();

从此节点获取值:

string series_title = node["series_title"].InnerText;
string series_image = node["series_image"].InnerText;