How to serialize?

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
        // Get all files in Documents
        List<string> dirs = FileHelper.GetFilesRecursive("S:\\bob.smith\\");
        foreach (string p in dirs)
        {
            Console.WriteLine(p);
        }
        // Write count
        Console.WriteLine("Count: {0}", dirs.Count);
        Console.Read();
    }
}

static class FileHelper
{
    public static List<string> GetFilesRecursive(string b)
    {
        // 1.
        // Store results in the file results list.
        List<string> result = new List<string>();

        // 2.
        // Store a stack of our directories.
        Stack<string> stack = new Stack<string>();

        // 3.
        // Add initial directory.
        stack.Push(b);

        // 4.
        // Continue while there are directories to process
        while (stack.Count > 0)
        {
            // A.
            // Get top directory
            string dir = stack.Pop();

            try
            {
                // B
                // Add all files at this directory to the result List.
                result.AddRange(Directory.GetFiles(dir, "*.*"));

                // C
                // Add all directories at this directory.
                foreach (string dn in Directory.GetDirectories(dir))
                {
                    stack.Push(dn);
                }
            }
            catch
            {
                // D
                // Could not open the directory
            }
        }
        return result;
        var k = "";
        result = k;
    }
}

This is my code. I am trying to serialize the result to an XML file (I thought by passing it to var, I could use this variable to serialize). I am in the end. Please, help.

strange for some reason I can’t add comments - in response, yes, I went through endless lessons on serialization. I am a complete newbie to this material and, frankly, these tutorials were useless for most of b / c, I cannot use this function and serialize together. at the moment, I am looking for a solution, as I am going to seriously break something out of frustration.

+3
source share
4 answers

You can use XmlSerializer:

using System.Xml.Serialization;

static void Main()
    {
        // Get all files in Documents
        List<string> dirs = FileHelper.GetFilesRecursive(@"S:\\bob.smith\\");

        XmlSerializer x = new XmlSerializer(dirs.GetType());
        x.Serialize(Console.Out, dirs);



        Console.Read();
    }
+7

dirs, , , XmlWriter:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(@"c:\path\filename.xml", settings))
{
    writer.WriteStartElement("dirs");
    dirs.ForEach(d => writer.WriteElementString("dir", d));
    writer.WriteEndElement(); // dirs
}

:

<?xml version="1.0" encoding="utf-16"?>
<dirs>
  <dir>c:\</dir>
  <dir>c:\Program Files</dir>
  ...
</dirs>

, , .NET, , , , , xml .

"-", BinaryFormatter:

// serialize the object to disk
BinaryFormatter formatter = new BinaryFormatter();
using (Stream stream = File.OpenWrite(@"c:\temp\dirlist.data"))
{
    formatter.Serialize(stream, dirs);
}

// at some other point, when you want to deserialize
BinaryFormatter formatter = new BinaryFormatter();
List<string> dirList;
using (Stream stream = File.OpenRead(@"c:\temp\dirlist.data"))
{
    dirList = (List<string>)formatter.Deserialize(stream);
}

, ( : , "" ):

ÿÿÿÿ          System.Collections.Generic.List`1[[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]   
_items_size_version                       c:\   c:\Program Files
+3

, , , , , .

. , , SO.

, ,

+2

,

Directory.GetFiles(@"S:\\bob.smith\\", "*.*", SearchOption.AllDirectories)
+1

Source: https://habr.com/ru/post/1710087/


All Articles