Home > Blockchain >  Display XML content inside a GridView (Windows Form)
Display XML content inside a GridView (Windows Form)

Time:05-26

Ive been trying to display every content of a selected xml file inside a gridview on a Windows Form, but for some reason, only the content of the first parent node is shown (Playlist). I cant find a solution for this.

Heres my code:

private void OpenMontagem_btn(object sender, EventArgs e)
    {
        var fileContent = string.Empty;
        var filePath = string.Empty;

        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            openFileDialog.InitialDirectory = @"C:\Playlist\pgm\Montagem\";
            openFileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;

                var fileStream = openFileDialog.OpenFile();

                using (StreamReader reader = new StreamReader(fileStream))
                {
                    fileContent = reader.ReadToEnd();
                }
            }
        }

        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(filePath);
            MontagemGrid.DataSource = ds.Tables[0];
        }
    }

Heres a piece of the xml file

<?xml version="1.0" encoding="Windows-1252"?>
<Playlist CompactMode="1">
    <Break0000M Ins="14" Locked="0" Changed="0" Filetime="2021-09-28T06:11:23.509Z" Filetime2="2021-09-22T21:18:17.190Z" Time="2021-09-29T00:00:00.000Z" Type="2" Id="" Id_Edited="" Fixo="0" Descarte="0" Net="1" Dur="0" Slots="3" Orig="00:00 &quot;Jane's Addiction - Jane Says (ao vivo).mp3&quot;, &quot;G. Love - Home.mp3&quot;, &quot;Mumford &amp; Sons - Hopeless Wanderer.mp3&quot;, &quot;BACHMAN TURNER OVERDRIVER = HEY YOY.mp3&quot;, &quot;Maná - Labios Compartidos.mp3&quot;, &quot;MAGIC! - No Way No.mp3&quot;, &quot;Maneva - Eu Te Devoro - Ao Vivo.mp3&quot;, &quot;3 Doors Down - Behind those eyes.mp3&quot;, &quot;Queen – Bohemian Rhapsody.mp3&quot;, &quot;Sleeping At Last - Saturn.mp3&quot;, &quot;BAD COMPANY - IF YOU NEED SOMEBODY.mp3&quot;, &quot;Justin Timberlake - Mirrors.mp3&quot;, &quot;Avril Lavigne - Girlfriend.mp3&quot;, &quot;Maneva - O Vento - Ao Vivo.mp3&quot;, ">
        <Ins0 Id="" CTA="" Source="M" Type="M;F;" Title="Jane's Addiction - Jane Says (ao vivo)" File="Jane's Addiction - Jane Says (ao vivo).mp3" Folder="Rock" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2011-21-10T14:08:47" HoraPK="2021-23-09T15:43:21" IsAudioFile="1" Dur="384773" Refr="0" DurRefr="0" PtVh="0" PtMx="377680" MxIni="5003" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins1 Id="" CTA="" Source="M" Type="M;F;" Title="G. Love - Home" File="G. Love - Home.mp3" Folder="Rock" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2011-10-06T03:22:56" HoraPK="2021-23-09T15:41:50" IsAudioFile="1" Dur="219874" Refr="0" DurRefr="0" PtVh="0" PtMx="211706" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="320" Reg="0"/>
        <Ins2 Id="" CTA="" Source="M" Type="M;F;" Title="Mumford &amp; Sons - Hopeless Wanderer" File="Mumford &amp; Sons - Hopeless Wanderer.mp3" Folder="Alternativa" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-23-09T15:19:50" HoraPK="2021-23-09T15:27:29" IsAudioFile="1" Dur="307999" Refr="0" DurRefr="0" PtVh="0" PtMx="301840" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins3 Id="" CTA="" Source="M" Type="M;F;" Title="BACHMAN TURNER OVERDRIVER = HEY YOY" File="BACHMAN TURNER OVERDRIVER = HEY YOY.mp3" Folder="Flashback" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2011-17-11T18:40:01" HoraPK="2021-23-09T15:44:14" IsAudioFile="1" Dur="207621" Refr="0" DurRefr="0" PtVh="0" PtMx="199447" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="64" Reg="0"/>
        <Ins4 Id="" CTA="" Source="M" Type="M;F;" Title="Maná - Labios Compartidos" File="Maná - Labios Compartidos.mp3" Folder="Pop" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-23-09T15:19:51" HoraPK="2021-23-09T15:30:11" IsAudioFile="1" Dur="315004" Refr="0" DurRefr="0" PtVh="0" PtMx="305559" MxIni="4027" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins5 Id="" CTA="" Source="M" Type="M;F;" Title="MAGIC! - No Way No" File="MAGIC! - No Way No.mp3" Folder="Pop" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-23-09T15:19:53" HoraPK="2021-23-09T15:29:37" IsAudioFile="1" Dur="229441" Refr="0" DurRefr="0" PtVh="0" PtMx="225609" MxIni="417" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins6 Id="" CTA="" Source="M" Type="M;F;" Title="Maneva - Eu Te Devoro - Ao Vivo" File="Maneva - Eu Te Devoro - Ao Vivo.mp3" Folder="Reggae" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-22-09T15:10:24" HoraPK="2021-22-09T18:06:36" IsAudioFile="1" Dur="240298" Refr="0" DurRefr="0" PtVh="0" PtMx="234308" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="320" Reg="0"/>
        <Ins7 Id="" CTA="" Source="M" Type="M;F;" Title="3 Doors Down - Behind those eyes" File="3 Doors Down - Behind those eyes.mp3" Folder="Rock" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2012-06-03T16:33:26" HoraPK="2021-22-09T18:05:27" IsAudioFile="1" Dur="254630" Refr="0" DurRefr="0" PtVh="0" PtMx="247461" MxIni="886" Intro="0" PtLoc="0" Vol="0" Bitrate="160" Reg="0"/>
        <Ins8 Id="" CTA="" Source="M" Type="M;F;" Title="Queen – Bohemian Rhapsody" File="Queen – Bohemian Rhapsody.mp3" Folder="Rock" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-22-09T16:11:40" HoraPK="2021-22-09T18:06:13" IsAudioFile="1" Dur="24" Refr="0" DurRefr="0" PtVh="0" PtMx="0" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="112" Reg="0"/>
        <Ins9 Id="" CTA="" Source="M" Type="M;F;" Title="Sleeping At Last - Saturn" File="Sleeping At Last - Saturn.mp3" Folder="Alternativa" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-23-09T13:36:15" HoraPK="2021-23-09T13:36:41" IsAudioFile="1" Dur="285272" Refr="0" DurRefr="0" PtVh="0" PtMx="277453" MxIni="10976" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins10 Id="" CTA="" Source="M" Type="M;F;" Title="BAD COMPANY - IF YOU NEED SOMEBODY" File="BAD COMPANY - IF YOU NEED SOMEBODY.mp3" Folder="Flashback" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2011-21-10T14:07:14" HoraPK="2021-23-09T15:44:16" IsAudioFile="1" Dur="255043" Refr="0" DurRefr="0" PtVh="0" PtMx="246876" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="256" Reg="0"/>
        <Ins11 Id="" CTA="" Source="M" Type="M;F;" Title="Justin Timberlake - Mirrors" File="Justin Timberlake - Mirrors.mp3" Folder="Pop" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-23-09T15:19:53" HoraPK="2021-23-09T15:29:23" IsAudioFile="1" Dur="481519" Refr="0" DurRefr="0" PtVh="0" PtMx="477049" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins12 Id="" CTA="" Source="M" Type="M;F;" Title="Avril Lavigne - Girlfriend" File="Avril Lavigne - Girlfriend.mp3" Folder="Pop Rock" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2014-27-05T20:54:31" HoraPK="2021-22-09T18:06:25" IsAudioFile="1" Dur="214194" Refr="0" DurRefr="0" PtVh="0" PtMx="212615" MxIni="1032" Intro="0" PtLoc="0" Vol="0" Bitrate="160" Reg="0"/>
        <Ins13 Id="" CTA="" Source="M" Type="M;F;" Title="Maneva - O Vento - Ao Vivo" File="Maneva - O Vento - Ao Vivo.mp3" Folder="Reggae" Text="" Composer="" Comment="" Checked="1" Err="0" sErr="" HoraAudio="2021-22-09T15:10:24" HoraPK="2021-22-09T18:06:42" IsAudioFile="1" Dur="202162" Refr="0" DurRefr="0" PtVh="0" PtMx="201789" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="320" Reg="0"/>
    </Break0000M>
    <Break0058C Ins="3" Locked="0" Changed="0" Filetime="2021-09-24T17:15:19.255Z" Filetime2="2021-09-22T21:20:18.519Z" Time="2021-09-29T00:58:00.000Z" Type="1" Id="" Id_Edited="" Fixo="0" Descarte="0" Net="1" Dur="0" Slots="3" Orig="00:58 3, 9, 4">
        <Ins0 Id="3" CTA="" Source="C" Type="C;" Title="Banco Santander Ser Dono" File="Banco Santander Ser Dono.MP3" Folder="Comerciais" Text="" Checked="1" Err="0" sErr="" HoraAudio="2018-09-04T17:01:06" HoraPK="2021-01-05T03:03:20" IsAudioFile="1" Dur="28701" Refr="0" DurRefr="0" PtVh="0" PtMx="28357" MxIni="0" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins1 Id="9" CTA="" Source="C" Type="C;" Title="Claro" File="Claro.MP3" Folder="Comerciais" Text="" Checked="1" Err="0" sErr="" HoraAudio="2018-09-04T15:05:35" HoraPK="2018-10-04T13:08:36" IsAudioFile="1" Dur="29920" Refr="0" DurRefr="0" PtVh="0" PtMx="28664" MxIni="512" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
        <Ins2 Id="4" CTA="" Source="C" Type="C;" Title="Bobs Hino dos Lambuzados" File="Bobs Hino dos Lambuzados.MP3" Folder="Comerciais" Text="" Checked="1" Err="0" sErr="" HoraAudio="2018-10-04T17:13:21" HoraPK="2021-15-06T14:03:51" IsAudioFile="1" Dur="60600" Refr="0" DurRefr="0" PtVh="0" PtMx="60200" MxIni="456" Intro="0" PtLoc="0" Vol="0" Bitrate="128" Reg="0"/>
    </Break0058C>
</Playlist>

If I change ds.Tables[0] to ds.Tables[1] it shows every node with the name of Ins0

Is there a way to structure and display every bit of content inside the gridview?

CodePudding user response:

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication27
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Break Folder", typeof(string));
            dt.Columns.Add("Ins", typeof(int));
            dt.Columns.Add("Locked", typeof(int));
            dt.Columns.Add("Changed", typeof(int));
            dt.Columns.Add("FileTime", typeof(DateTime));
            dt.Columns.Add("Filetime2", typeof(DateTime));
            dt.Columns.Add("Time", typeof(DateTime));
            dt.Columns.Add("Folder_Type", typeof(int));
            dt.Columns.Add("Folder_Id", typeof(string));
            dt.Columns.Add("Id_Edited", typeof(string));
            dt.Columns.Add("Fixo", typeof(int));
            dt.Columns.Add("Descarte", typeof(int));
            dt.Columns.Add("Net", typeof(int));
            dt.Columns.Add("Break Dur", typeof(int));
            dt.Columns.Add("Slots", typeof(int));
            dt.Columns.Add("Orig", typeof(string));

            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("CTA", typeof(string));
            dt.Columns.Add("Soruce", typeof(string));
            dt.Columns.Add("Type", typeof(string));
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("File", typeof(string));
            dt.Columns.Add("Folder", typeof(string));
            dt.Columns.Add("Text", typeof(string));
            dt.Columns.Add("Composer", typeof(string));
            dt.Columns.Add("Comment", typeof(string));
            dt.Columns.Add("Checked", typeof(int));
            dt.Columns.Add("Err", typeof(int));
            dt.Columns.Add("sErr", typeof(string));
            dt.Columns.Add("HoraAudio", typeof(DateTime));
            dt.Columns.Add("HoraPK", typeof(DateTime));
            dt.Columns.Add("IsAudioFile", typeof(int));
            dt.Columns.Add("Dur", typeof(int));
            dt.Columns.Add("Refr", typeof(int));
            dt.Columns.Add("DurRefr", typeof(int));
            dt.Columns.Add("PtVh", typeof(int));
            dt.Columns.Add("OtMx", typeof(int));
            dt.Columns.Add("MxIni", typeof(int));
            dt.Columns.Add("Intro", typeof(int));
            dt.Columns.Add("PtLoc", typeof(int));
            dt.Columns.Add("Vol", typeof(int));
            dt.Columns.Add("Bitrate", typeof(int));
            dt.Columns.Add("Reg", typeof(int));

            XElement playList = doc.Descendants("Playlist").FirstOrDefault();

            foreach (XElement brk in playList.Elements())                {
                string breakFolder = brk.Name.LocalName;
                int ins = (int)brk.Attribute("Ins");
                int locked = (int)brk.Attribute("Locked");
                int changed = (int)brk.Attribute("Locked");
                DateTime filetime = (DateTime)brk.Attribute("Filetime");
                DateTime filetime2 = (DateTime)brk.Attribute("Filetime2");
                DateTime time = (DateTime)brk.Attribute("Time");
                string folderType = (string)brk.Attribute("Type");
                string folderId = (string)brk.Attribute("Id");
                string id_Edited = (string)brk.Attribute("Id_Edited");
                int fixo = (int)brk.Attribute("Fixo");
                int descarte = (int)brk.Attribute("Descarte");
                int net = (int)brk.Attribute("Net");
                int breakDur = (int)brk.Attribute("Dur");
                int slots = (int)brk.Attribute("Slots");
                string orig = (string)brk.Attribute("Orig");


                foreach (XElement insNum in brk.Elements())
                {
                    DataRow row = dt.Rows.Add();
                    //Id,CTA,Source,Type,Title,File,Folder,Text,Composer,Comment,Checked,Err,sErr,HoraAudio,HoraPK,IsAudioFile,Dur,Refr,DurRefr,PtVh,PtMx,MxIni,Intro,PtLoc,Vol,Bitrate,Reg
                    string id = (string)insNum.Attribute("Id");
                    string cta = (string)insNum.Attribute("CTA");
                    string source = (string)insNum.Attribute("Source");
                    string type = (string)insNum.Attribute("Type");
                    string title = (string)insNum.Attribute("Title");
                    string file = (string)insNum.Attribute("File");
                    string folder = (string)insNum.Attribute("Folder");
                    string text = (string)insNum.Attribute("Text");
                    string composer = (string)insNum.Attribute("Composer");
                    string comment = (string)insNum.Attribute("Comment");
                    int check = (int)insNum.Attribute("Checked");
                    int err = (int)insNum.Attribute("Err");
                    string sErr = (string)insNum.Attribute("sErr");
                    DateTime horaAudio = DateTime.ParseExact((string)insNum.Attribute("HoraAudio"), "yyyy-dd-MMTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
                    DateTime horaPK = DateTime.ParseExact((string)insNum.Attribute("HoraPK"), "yyyy-dd-MMTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
                    int isAudioFile = (int)insNum.Attribute("Err");
                    int dur = (int)insNum.Attribute("Dur");
                    int refr = (int)insNum.Attribute("Refr");
                    int durRefr= (int)insNum.Attribute("DurRefr");
                    int ptVh = (int)insNum.Attribute("PtVh");
                    int ptMx = (int)insNum.Attribute("PtMx");
                    int mxIni = (int)insNum.Attribute("MxIni");
                    int intro = (int)insNum.Attribute("Intro");
                    int ptloc = (int)insNum.Attribute("PtLoc");
                    int vol = (int)insNum.Attribute("Vol");
                    int bitrate = (int)insNum.Attribute("Bitrate");
                    int reg = (int)insNum.Attribute("Reg");

                    row.ItemArray = new object[] { breakFolder, ins, locked, changed, filetime, filetime2, time, folderType,
                                                   folderId, id_Edited, fixo, descarte, net, breakDur,slots, orig,
                                                   id, cta, source,type, title, file, folder, text, composer, comment,
                                                   check, err,sErr, horaAudio, horaPK,isAudioFile, dur, refr,
                                                   durRefr,ptVh,ptMx, mxIni, ptloc, vol, bitrate, reg
                    };
                    
                }
            }

        }
    }
}
  • Related