0

I know there are similar questions here but my XML is kind of specific and I cant seem to figure out how display it properly. So, I'm generating XML out of folder. It generates all subfolders and files and files in subfolders and everything into XML. Thing is, the dir needs to display number of files, and file needs to display its size, beside their names. To make this more clear, here's a XML from test folder:

<?xml version="1.0" encoding="utf-8"?>
<dir folder_name="Test">
  <dir number_of_files="4" />
  <file name="input.txt" />
  <file size="294" />
  <file name="New Text Document - Copy (2).txt" />
  <file size="12" />
  <file name="New Text Document - Copy.txt" />
  <file size="16" />
  <file name="New Text Document.txt" />
  <file size="6" />
  <dir folder_name="DisplayXML">
    <dir number_of_files="6" />
    <file name="App.config" />
    <file size="189" />
    <file name="DisplayXML.csproj" />
    <file size="4133" />
    <file name="Form1.cs" />
    <file size="527" />
    <file name="Form1.Designer.cs" />
    <file size="2059" />
    <file name="Form1.resx" />
    <file size="5817" />
    <file name="Program.cs" />
    <file size="565" />
    <dir folder_name="bin">
      <dir number_of_files="0" />
      <dir folder_name="Debug">
        <dir number_of_files="8" />
        <file name="CreateXML.dll" />
        <file size="6144" />
        <file name="CreateXML.pdb" />
        <file size="15872" />
        <file name="DisplayXML.exe" />
        <file size="8192" />
        <file name="DisplayXML.exe.config" />
        <file size="189" />
        <file name="DisplayXML.pdb" />
        <file size="19968" />
        <file name="DisplayXML.vshost.exe" />
        <file size="22696" />
        <file name="DisplayXML.vshost.exe.config" />
        <file size="189" />
        <file name="DisplayXML.vshost.exe.manifest" />
        <file size="490" />
      </dir>
    </dir>
    <dir folder_name="obj">
      <dir number_of_files="0" />
      <dir folder_name="Debug">
        <dir number_of_files="12" />
        <file name="DesignTimeResolveAssemblyReferences.cache" />
        <file size="1464" />
        <file name="DesignTimeResolveAssemblyReferencesInput.cache" />
        <file size="7394" />
        <file name="DisplayXML.csproj.FileListAbsolute.txt" />
        <file size="1203" />
        <file name="DisplayXML.csproj.GenerateResource.Cache" />
        <file size="1012" />
        <file name="DisplayXML.csprojResolveAssemblyReference.cache" />
        <file size="6336" />
        <file name="DisplayXML.exe" />
        <file size="8192" />
        <file name="DisplayXML.Form1.resources" />
        <file size="180" />
        <file name="DisplayXML.pdb" />
        <file size="19968" />
        <file name="DisplayXML.Properties.Resources.resources" />
        <file size="180" />
        <file name="TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs" />
        <file size="0" />
        <file name="TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs" />
        <file size="0" />
        <file name="TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs" />
        <file size="0" />
        <dir folder_name="TempPE">
          <dir number_of_files="0" />
        </dir>
      </dir>
    </dir>
    <dir folder_name="Properties">
      <dir number_of_files="5" />
      <file name="AssemblyInfo.cs" />
      <file size="1432" />
      <file name="Resources.Designer.cs" />
      <file size="2850" />
      <file name="Resources.resx" />
      <file size="5612" />
      <file name="Settings.Designer.cs" />
      <file size="1097" />
      <file name="Settings.settings" />
      <file size="249" />
    </dir>
  </dir>
  <dir folder_name="New folder">
    <dir number_of_files="5" />
    <file name="New Text Document - Copy (2) - Copy.txt" />
    <file size="12" />
    <file name="New Text Document - Copy (2).txt" />
    <file size="12" />
    <file name="New Text Document - Copy (3) - Copy.txt" />
    <file size="12" />
    <file name="New Text Document - Copy (4) - Copy.txt" />
    <file size="12" />
    <file name="New Text Document - Copy (5) - Copy.txt" />
    <file size="12" />
  </dir>
  <dir folder_name="New folder - Copy">
    <dir number_of_files="0" />
  </dir>
  <dir folder_name="New folder - Copy (2)">
    <dir number_of_files="0" />
  </dir>
  <dir folder_name="New folder - Copy (3)">
    <dir number_of_files="0" />
  </dir>
  <dir folder_name="New folder - Copy (4)">
    <dir number_of_files="0" />
  </dir>
</dir>

Sorry if the XML is a bit 2 large. So, let's say I will be loading this XML when I press a button, into a DataGridView

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
       // need code here to load all xml fiels (dir name for a relevant file,   file name, file size)
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }
}

How do I customize what I want to display in a grid?

Marko
  • 917
  • 9
  • 14
  • Ok I was using Xatribute to create for example I just changed it to Xelement and it displays as CreateXML.pdb Following this i might be able to achieve to create and load a normal xml :) Will post the answer – Marko Apr 14 '16 at 18:21
  • It would be better to display in a tree view since you have recursive folders. Try the code on following posting. Use the updates which displays the attributes (in your case the file sizes : http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Apr 14 '16 at 18:34
  • I have managed to fix my entire XML structure into a tree so now it looks nice. Now ill search for simple implementation intro grid and I will post if someone cares to advice :) Ty for answer tho – Marko Apr 14 '16 at 18:45
  • Do you plan on having one column contain folder name? You can use the link I provided to parse through the xml file and then add to gridview instead of treeview. Not very hard to modify. – jdweng Apr 14 '16 at 18:52
  • Doesn't 'Size' of the file need to be an attribute with the name else your going to struggle to match Name and Size... EG. – Monty Apr 14 '16 at 19:07
  • jdweng wont be needing this because i'll first generate XML through my method, and then ill load it into gridview. working on it monty it doesnt because DataSet.Tables[0] returns something then [1] returns another thing im just trying to figure out now how to show them all – Marko Apr 14 '16 at 20:14
  • @jdweng you were right :) It didnt work in a grid, I had to populate a TreeView – Marko Apr 14 '16 at 22:52

0 Answers0