【问题标题】:Saving XML file to disk将 XML 文件保存到磁盘
【发布时间】:2016-07-22 07:47:29
【问题描述】:

我正在尝试将 XDocument 对象作为 .xml 文件保存到磁盘。该对象在调试器中似乎格式良好,我只是在保存实际文件时遇到问题。这是我正在做的事情:

       //ofx is an `XElement` containing many sub-elements of `XElement` type
        XDocument document = new XDocument(ofx);

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.Indent = true;

        //destinationPath is a string containing a path, like this
        // e.g : "C:\\Users\\Myself\\Desktop"  
        using (var stream = File.Create(destionationPath + @"export.xml"))
        {
            List<byte[]> header = new List<byte[]>();

            byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);

            List<string> headers = new List<string>();
            headers.Add("foo"); 

            // Just adding some headers here on the top of the file. 
            // I'm pretty sure this is irrelevant for my problem

            foreach (string item in headers)
            {
                header.Add(Encoding.ASCII.GetBytes(item));
                header.Add(newline);
            }

            header.Add(newline);

            byte[] bytes = header.SelectMany(a => a).ToArray();

            stream.Write(bytes, 0, bytes.Length);
            using (var writer = XmlWriter.Create(stream, settings))
            {
                document.Save(writer);
            }

        }

每当我调用它时,目标目录中都没有文件。

注意:目录和文件名也是格式正确的。这在代码中得到了验证。

【问题讨论】:

    标签: c# xml file save disk


    【解决方案1】:

    您可能正在错误地创建路径。按照你的评论

    string destionationPath = "C:\\Users\\Myself\\Desktop";
    destionationPath + @"export.xml" // evaluates to: "C:\\Users\\Myself\\Desktopexport.xml"
    

    您在目录和文件名之间缺少"\\"

    以下代码对我来说很好用。

    namespace TestApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Tmp();
            }
    
            public static void Tmp()
            {
    
                //ofx is an `XElement` containing many sub-elements of `XElement` type
                XDocument document = new XDocument(new XElement("myName", "myContent"));
    
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Indent = true;
    
                //destinationPath is a string containing a path, like this
                // e.g : "C:\\Users\\Myself\\Desktop"  
                string destionationPath = "D:\\Temp\\xml_test\\";
                using (var stream = File.Create(destionationPath + @"export.xml"))
                {
                    List<byte[]> header = new List<byte[]>();
    
                    byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
    
                    List<string> headers = new List<string>();
                    headers.Add("foo");
    
                    // Just adding some headers here on the top of the file. 
                    // I'm pretty sure this is irrelevant for my problem
    
                    foreach (string item in headers)
                    {
                        header.Add(Encoding.ASCII.GetBytes(item));
                        header.Add(newline);
                    }
    
                    header.Add(newline);
    
                    byte[] bytes = header.SelectMany(a => a).ToArray();
    
                    stream.Write(bytes, 0, bytes.Length);
                    using (var writer = XmlWriter.Create(stream, settings))
                    {
                        document.Save(writer);
                    }
                }
            }
        }
    }
    

    为了避免这种情况永远不要由你自己创建创建路径。改用Path 类:

    Path.Combine(destionationPath, @"export.xml")
    

    【讨论】:

    • 嗯,就是这样!谢谢男人;)很好的答案和很好的解释
    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 2010-09-23
    • 2010-11-08
    • 2021-11-03
    • 2019-10-30
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多