将矩阵插入XML

时间:2016-01-17 21:30:55

标签: c# xml matrix multidimensional-array

我想将Matrix中的数据插入XML。我怎么能这样做?

internal static XDocument createData(string[,] banciforxml)
{
    XDocument Bancile = new XDocument();
    Bancile.Add(new XElement("bancks", 
                             banciforxml.Select(x => new XElement("item", x))));
    return Bancile;
}

2 个答案:

答案 0 :(得分:0)

你应该这样做:

public XDocument createData(string[,] banciforxml)
{
    XDocument Bancile = new XDocument();

    XElement root = new XElement("banci");

    Bancile.Add(root);

    //Go through the first dimension of the array
    for (int i = banciforxml.GetLowerBound(0); i <= banciforxml.GetUpperBound(0); i++)
    {
        var d1element = new XElement("bancks");

        root.Add(d1element);

        //Go though the second dimension of the array
        for (int j = banciforxml.GetLowerBound(1); j <= banciforxml.GetUpperBound(1); j++)
        {
            var value = banciforxml[i, j];

            if(value == null)
                continue;

            var d2element = new XElement("item" + (j+1));

            d2element.Value = value;

            d1element.Add(d2element);
        }
    }

    return Bancile;
}

此代码使用GetLowerBoundGetUpperBound方法获取数组两个维度中每个维度的下部和上部索引。

答案 1 :(得分:0)

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        HtmlWeb ht = new HtmlWeb();
        //XDocument Mydocument = Data.createData();
        //MessageBox.Show(Mydocument.ToString());
        //Data.SaveData("myfirst.xml");
        try
        {
            doc = ht.Load("http://www.curs.md/curs_valutar_banci");
        }

        catch 
        { 
            MessageBox.Show("Eroare la conectare cu sursa...");
            Close();
        }
        string[] result = new string[50];
        string[] banci = new string[] { "bnm", "eneg", "ecb", "exim", "fin", "mobi", "micb", "maib", "proc", "vb" };
        string[,] banciforxml=new string[10,15];
        for (int i = 0; i < banci.Length; i++)
        {
            try
            {
                HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//table/tbody/tr[@class='" + banci[i] + "']/td");
                for (int x = 0; x < 15; x++)
                {
                    result[x] = nodes[x].InnerText;
                    banciforxml[i,x]=result[x];

                    listBox1.Items.Add("Result "+i+":"+x+" -> "+banciforxml[i,x]);
                }
            }
            catch
            {
                string value = banci[i];
                switch (value)
                {
                    case "bnm":
                        MessageBox.Show("Banca Nationala nu functioneaza!!");
                        continue;
                    case "eneg":
                        MessageBox.Show("Energbank nu functioneaza!!");
                        continue;
                    case "ecb":
                        MessageBox.Show("EuroCreditBank nu functioneaza!!");
                        continue;
                    case "exim":
                        MessageBox.Show("Eximbank nu functioneaza!!");
                        continue;
                    case "fin":
                        MessageBox.Show("FinComBank nu functioneaza!!");
                        continue;
                    case "mobi":
                        MessageBox.Show("Mobiasbanca nu functioneaza!!");
                        continue;
                    case "micb":
                        MessageBox.Show("Moldindconbank nu functioneaza!!");
                        continue;
                    case "maib":
                        MessageBox.Show("Moldova Agroindbank nu functioneaza!!");
                        continue;
                    case "proc":
                        MessageBox.Show("ProCredit Bank nu functioneaza!!");
                        continue;
                    case "vb":
                        MessageBox.Show("Victoriabank nu functioneaza!!");
                        continue;
                }

            }

        }
        XDocument myDocument=Data.createData(banciforxml);//<-Here is the problem
        listBox1.Items.Add(myDocument);