首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C#中获取文件目录并在xml中显示

如何在C#中获取文件目录并在xml中显示
EN

Stack Overflow用户
提问于 2016-03-12 09:38:09
回答 3查看 1.5K关注 0票数 2

这是我想要的cod,在目录中获取所有文件,在xml文件中结束写入所有文件

代码语言:javascript
复制
  private void button3_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();

        string appPath = Path.GetDirectoryName(Application.ExecutablePath);

        string folder = appPath;//Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Archive\";
        string filter = "*.*";
        string[] files = Directory.GetFiles(folder, filter);
        foreach (string item in files)
        {

            string string1 = item;
            string string2 = appPath;
            string  result = string1.Replace(string2, "");
            MessageBox.Show(result);
            doc.LoadXml("<item><name>@" + result + " </name></item>");
            // Save the document to a file and auto-indent the output.
            using (XmlTextWriter writer = new XmlTextWriter("data.xml", null))

            {
                writer.Formatting = Formatting.Indented;
                doc.Save(writer);
                writer.Close();
            }
        }
    }

使用这段代码,我将我的文件放在目录中并删除路径,例如C://folder1 1/folder1 2/bin/ app.exe到app.exe,它是可以的,但最后在xml中只需编写一个XML结果

代码语言:javascript
复制
<?xml version="1.0"?>
<item>
  <name>@\WindowsFormsApplication8.vshost.exe.manifest </name>
</item>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-12 10:02:06

在此:

doc.LoadXml("<item><name>@" + result + " </name></item>");

每次循环重复时,都会覆盖XmlDocument中的所有XML。

如果您想要使用XmlDocument,请尝试使用它,尽管有其他(更清洁的)方法来输出XML。

代码语言:javascript
复制
        var doc = new XmlDocument();
        var root = doc.AppendChild(doc.CreateElement("Item"));

        foreach (var item in files)
        {
            var name = root.AppendChild(doc.CreateElement("Name"));
            name.InnerText = item;
        }


        var xmlWriterSettings = new XmlWriterSettings { Indent = true };
        using (var writer = XmlWriter.Create("data.xml", xmlWriterSettings))
        {
            doc.Save(writer);
        }

使用XmlSerialiser (比XDocument更干净的C#代码):

代码语言:javascript
复制
public class Program
{
    [XmlType("Item")]
    public class Item
    {
        [XmlElement("Name")]
        public string[] Files { get; set; }
    }

    static string SerialiseToXml<T>(T obj, bool isFormatted = false)
    {
        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        var stringBuilder = new StringBuilder();
        var xmlWriterSettings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = isFormatted };
        using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings))
        {
            var serializer = new XmlSerializer(obj.GetType());
            serializer.Serialize(xmlWriter, obj, ns);
            return stringBuilder.ToString();
        }
    }

    static void Main(string[] args)
    {
        string[] files = {"Apple.txt", "Orange.exe", "Pear.docx", "Banana.xml", "Papaya.xls", "Passionfruit.cs"};

        var item = new Item {Files = files};
        var xml = SerialiseToXml(item, true);

        Console.WriteLine(xml);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2016-03-12 10:36:05

你正在覆盖你的项目。

下面是编写适当xml的代码:

代码语言:javascript
复制
        XmlDocument doc = new XmlDocument();

        string appPath = Directory.GetCurrentDirectory();

        string folder = appPath;
        string filter = "*.*";
        string[] files = Directory.GetFiles(folder, filter);

        using (XmlTextWriter writer = new XmlTextWriter("data.xml", null))
        {

            writer.WriteStartDocument();

            writer.WriteStartElement("Items");

            foreach (string item in files)
            {
                string string1 = item;
                string string2 = appPath;
                string result = string1.Replace(string2, "");

                writer.WriteElementString("Item","", result);

                Console.WriteLine(result);

                writer.Formatting = Formatting.Indented;
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Close();
            doc.Save(writer);
        }

这是示例xml,

代码语言:javascript
复制
<?xml version="1.0"?>
<Items>
  <Item>\ConsoleApplication1.exe</Item>
  <Item>\ConsoleApplication1.exe.config</Item>
  <Item>\ConsoleApplication1.pdb</Item>
  <Item>\ConsoleApplication1.vshost.exe</Item>
  <Item>\ConsoleApplication1.vshost.exe.config</Item>
  <Item>\ConsoleApplication1.vshost.exe.manifest</Item>
  <Item>\data.xml</Item>
</Items>
票数 0
EN

Stack Overflow用户

发布于 2016-03-12 18:06:57

谢谢你最好的回答。我的目录中也有3个文件夹,任何文件夹中都有更多的文件,我希望文件夹中的任何文件都用xml 编写,例如

代码语言:javascript
复制
<Items>
  <Item>\ConsoleApplication1.exe</Item>
  <Item>\ConsoleApplication1.exe.config</Item>
  <Item>\ConsoleApplication1.pdb</Item>
  <Item>\ConsoleApplication1.vshost.exe</Item>
  <Item>\ConsoleApplication1.vshost.exe.config</Item>
<Item>..folder1\gold.dll</Item>
<Item>..images\exit.png</Item>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35955988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档