如何序列化? [英] How to serialize?

查看:143
本文介绍了如何序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
    }
}

这是我的代码。我想序列化的结果到一个XML文件(我想将它传递给一个变种,我可以使用该变量序列化)。我是黔驴技穷。请别人帮忙

This is my code. I am trying to serialize the result to an XML file (i thought by passing it to a var, i could use that variable to serialize). I'm at wits' end. Please someone help.

古怪出于某种原因,我不能添加注释 - 回应,是的,我已经走了过来约系列化无尽的教程。我是一个完整的新手在这个东西,坦白的说,这些教程已经于事无补了很大一部分B / C我不能把这个功能和serializaton在一起。在这一点上,我在找我都快要认真突破事出无奈的解决方案。

weirdly for some reason i can't add comments--in response, yes i have gone over endless tutorials about serialization. i'm a complete newbie at this stuff and frankly put, these tutorials have been unhelpful for the large part b/c i'm not able to put this function and serializaton together. at this point, i'm looking for a solution as i'm about to seriously break something out of frustration.

推荐答案

您可以使用XmlSerializer的:

You can use the 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();
    }

这篇关于如何序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆