在C#

时间:2017-03-13 09:20:13

标签: c# xml

我已经通过c#生成了xml文件,并希望保存在特定位置:

例如:我的文件//

这是我的代码:

protected void btnClose_Click(object sender, EventArgs e)
{
StringWriter stringwriter = new StringWriter();
            XmlTextWriter xmlwriter = new XmlTextWriter(stringwriter);
            xmlwriter.Formatting = Formatting.Indented;
            xmlwriter.WriteStartDocument();
            xmlwriter.WriteStartElement("root");
            xmlwriter.WriteStartElement("information");
            xmlwriter.WriteEndElement();
            xmlwriter.WriteEndDocument();
            XmlDocument docSave = new XmlDocument();
            docSave.LoadXml(stringwriter.ToString());
            //string dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            docSave.Save(Server.MapPath("~/xml/") + Guid.NewGuid().ToString() + ".xml");
}

在服务器中写什么>地图路径

我试过

docSave.Save(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments),"xml")+ Guid.NewGuid().ToString() + ".xml");

但该文件未保存到xml目录中。 任何解决方案?

2 个答案:

答案 0 :(得分:2)

如果要保存在文档中:

    StringWriter stringwriter = new StringWriter();
    XmlTextWriter xmlwriter = new XmlTextWriter(stringwriter);
    xmlwriter.Formatting = Formatting.Indented;
    xmlwriter.WriteStartDocument();
    xmlwriter.WriteStartElement("root");
    xmlwriter.WriteStartElement("information");
    xmlwriter.WriteEndElement();
    xmlwriter.WriteEndDocument();
    XmlDocument docSave = new XmlDocument();
    docSave.LoadXml(stringwriter.ToString());
    docSave.Save(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "xml") + Guid.NewGuid().ToString() + ".xml");

如果要保存在Documents / xml

    StringWriter stringwriter = new StringWriter();
    XmlTextWriter xmlwriter = new XmlTextWriter(stringwriter);
    xmlwriter.Formatting = Formatting.Indented;
    xmlwriter.WriteStartDocument();
    xmlwriter.WriteStartElement("root");
    xmlwriter.WriteStartElement("information");
    xmlwriter.WriteEndElement();
    xmlwriter.WriteEndDocument();
    XmlDocument docSave = new XmlDocument();
    docSave.LoadXml(stringwriter.ToString());
    System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "xml//"));
    file.Directory.Create();
    docSave.Save(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "xml//") + Guid.NewGuid().ToString() + ".xml");

答案 1 :(得分:0)

你错过了#34; \"在xml目录之后:

docSave.Save(System.IO.Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.MyDoc‌​uments),
"xml[here you miss: \\]")+  Guid.NewGuid().ToString() + ".xml");

您将在Documents目录中找到所有xml文件,所有文件应如下所示:xml [GUID] .xml

您应该以最简单的方式编写代码,以便轻松找到这些问题。 您可以编写如下代码:

string outputDirectory =   Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "xml");
string filename = string.Format("{0}.{1}", Guid.NewGuid().ToString(), "xml");
string fullFileName = Path.Combine(outputDirectory, filename);
docSave.Save(fullFileName);