简体   繁体   中英

Saving file in C#

I'm new to programming in C#. I want to create and download an xml file. I found this artical on creating the xml. I followed it and it works perfect. But I can't figure out how to save the file to my computer. I think it has to be inplemented someware here:

public static void Main()
{
    // Read and write purchase orders.
    Test t = new Test();
    t.CreatePO("po.xml");
    //I think here the file is ready to dowload
    t.ReadPO("po.xml");
}

As for the t.CreatePO("po.xml"); function I have exactly whats in the artical. From the artical I took the last example. The 'file' is created by a StreamWriter . Then it converts an object to a XML by using Serialize .

Any step in the right direction will help!

For your question, you want to save the file to your computer.

You could try the following code to get it.

// Creates an instance of the XmlSerializer class;
// specifies the type of object to serialize.
XmlSerializer serializer =
new XmlSerializer(typeof(PurchaseOrder));
//We can use absolute paths to store it anywhere on the computer
string xmlPath = @"D:\Task\";           
TextWriter writer = new StreamWriter(Path.Combine(xmlPath,filename));
PurchaseOrder po = new PurchaseOrder();

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM