简体   繁体   中英

Reading data from XML into an 2D array

I have a xml file looks like it.

<movielist>
<movie>
    <title>ABC </title>
    <year>2005</year>
    <length>120 min</length>
    <director>ABV</director>
    <rating>1</rating>
    <genre>AAA</genre>
    <genre>BBB</genre>
    <genre>CCC</genre>
    <actor>John</actor>
    <actor>TOM</actor>
</movie>
<movie>
    <title>cba</title>
    <year>2015</year>
    <length>220min</length>
    <director>ABV</director>
    <rating>1</rating>
    <genre>AAA</genre>
    <genre>BBB</genre>
    <genre>CCC</genre>
    <actor>John</actor>
    <actor>TOM</actor>
</movie>
<movielist>

I want to store in a 2D array for each movie like:

data[0][0] = "ABC "
data[0][1] = "2005"
...
and so on

next movie.
data[1][0] = "cba"
data[1][1] = "2015"
...

How can I do it? I were trying on this way.

while (reader.Read())
{
    type = reader.NodeType;

    if (type == XmlNodeType.Element)
    {


            if (reader.Name == "title")
            {
                reader.Read();
                data[i, 0] = reader.Value;
            }

a method to load XML file into DataTable , just pass the path of your . xml file to it

public static DataTable LoadDataTableFromXML(string Path)
{
      XmlDocument doc = new XmlDocument(); /* using System.Xml */
      doc.Load(Path);
      DataSet dataSet = new DataSet();
      dataSet.ReadXml(new StringReader(doc.InnerXml));
      return dataSet.Tables[0];
}

now the DataTable returned from the method has the data from your .xml file

DataTable dtMovieDetails = LoadDataTableFromXML(Server.MapPath("xml file path"));

string[,] data = new string[dtMovieDetails.Rows.Count,dtMovieDetails.Columns.Count]; /*dtMovieDetails.Rows.Count represents number of Rows and dtMovieDetails.Columns.Count represents number of Columns in your xml file*/

int row = 0; /* row counter */
foreach (DataRow dataRow in dtMovieDetails.Rows)
{
   for(int col = 0; col < dataRow.ItemArray.Count(); col++) /*col is columns counter */
   { 
     data[row, col] = dataRow[col].ToString(); 
   }
   row++;
}
        XmlReader xmlFile;
        xmlFile = XmlReader.Create("XMLPATH should be here", new   XmlReaderSettings());

        DataSet ds = new DataSet();
        ds.ReadXml(xmlFile);

        int row_count = ds.Tables[0].Rows.Count;
        int coloumn_count = ds.Tables[0].Columns.Count;
        string[,] data = new string[row_count,coloumn_count ];

        for (int i = 0; i < row_count; i++)
        {


            data[i, 0] = ds.Tables[0].Rows[i][0].ToString();
            data[i, 1] = ds.Tables[0].Rows[i][1].ToString();
            data[i, 2] = ds.Tables[0].Rows[i][2].ToString();
            data[i, 3] = ds.Tables[0].Rows[i][3].ToString();
            data[i, 4] = ds.Tables[0].Rows[i][4].ToString();
            data[i, 5] = ds.Tables[0].Rows[i][5].ToString();
            data[i, 6] = ds.Tables[0].Rows[i][6].ToString();
            data[i, 7] = ds.Tables[0].Rows[i][7].ToString();
            data[i, 8] = ds.Tables[0].Rows[i][8].ToString();
            data[i, 9] = ds.Tables[0].Rows[i][9].ToString();


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM