如何使用具有单独属性的列表,而不是将整个列表包含在单个属性下

时间:2019-08-02 10:03:45

标签: c# list linq serialization element

最近,我已经开始使用序列化了,我想序列化一些数据。这些数据基本上是有关一组子目录的一些信息。 截至目前,当我序列化子目录的数据时,所有这些都位于一个标签下,我想知道是否有可能为每个子目录使用单独的标签。 我正在使用LINQ进行序列化

我当前的代码,

XDocument xmlDoc = XDocument.Load(@"C:\Users\Anon\AppData\Roaming\Test\Settings.xml");
                        xmlDoc.Element("Iniatial").Element("InitialAttribute").Add(
                            new XElement("Name", RepositoryName),
                            new XElement("Filespath", fullFilePath),
                               );

                        xmlDoc.Save(@"C:\Users\Anon\AppData\Roaming\Test\Settings.xml");

我的当前输出是,

<Main>

<folder>
    <Name>M1</Name>
    <Filespath>D:\HomeTest\somefolder\file</Filespath>

   <Name>M2</Name>
    <Filespath>D:\RHomeTest\somefolder\file</Filespath>

   <Name>M3</Name>
    <Filespath>D:\HomeTest\somefolder\file</Filespath>

</folder>

</Main>

必填输出为

<Main>

<folder  >
    <Name>M1</Name>
    <Filespath>D:\RepositoryHomeTest\smertereg\opts</Filespath>
</folder  >

<folder  >
    <Name>M2</Name>
    <Filespath>D:\RepositoryHomeTest\sykemelding\opts_2</Filespath>

</folder  >  


<folder  >
    <Name>M3</Name>
    <Filespath>D:\RepositoryHomeTest\sykemelding-2\opts_3</Filespath>


</folder  >
</Main>

如所需输出中所示,我想用一个额外的元素标签分隔这些项目

1 个答案:

答案 0 :(得分:0)

在下面查看我的代码。

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

namespace SAveDirectoriesXml
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            Console.WriteLine("Enter Return");
            Console.ReadLine();

        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());


                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }

            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error",errorMsg);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                        Console.WriteLine(errorMsg);
                        writer.WriteElementString("Error", errorMsg);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}