0

I don't want to store an .XML file to a location on the hard drive (that I need to care about); I want it to be stored beneath the project's App_Data folder.

I will need a multitude of these, with names that will be calculated at runtime, such as "gramps_minority_201509.xml", "hijomio_carta_20151101_20151205.xml", etc.

I want to save the contents of DataTables to these XML files and later read from them when rendering pages (the XML file contents will populate grids on the pages).

XML files can be added at design time to the App_Data folder by selecting Add -> New Item... -> Visual C# -> Data from App_Data folder context menu, and then selecting "XML File" from the list there.

However, at that point you provide a name for the XML file; as noted, I need to names these "on the fly" and there will ultimately be many thousands of them.

So how can I create these XML files in code, saving the contents of DataTables to them?

UPDATE

Or perhaps saving to .json would be even better...

fionaredmond
  • 639
  • 7
  • 15
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    App_Data folder is a location on your hard drive, believe it or not. Can you explain your scenario a bit more? You say that the names are "on the fly" can you add a bit more details, as to where these names come from? – Andrew Savinykh Dec 30 '15 at 21:08
  • I reckoned, that's why I added "(that I need to care about)". As to the names, their ilk is in the post; I will be able to know what they are due to a naming convention. – B. Clay Shannon-B. Crow Raven Dec 30 '15 at 21:10
  • If all the files are known at compile time, you can add them all as you described in the question. If there are too many of them too add to the solution, you can just copy them to the appropriate folder during deployment. If you get stuck with the file system limitation becoming too slow when there are more than 1000 in a folder you need to look at an alternate solution, such as database, or splitting these up in multiple folders. – Andrew Savinykh Dec 30 '15 at 21:12
  • As mentioned ("...names that will be calculated at runtime"), they are created at runtime. They are NOT known at compile time. Nor would I want to manually add 42gazillion files. – B. Clay Shannon-B. Crow Raven Dec 30 '15 at 21:13
  • And if all files are not known at compile time, please explain where they come from, as this is important for understanding the full extent of your question. You might be dealing with XY here. – Andrew Savinykh Dec 30 '15 at 21:13

1 Answers1

2

I would strongly advise you to use JSON instead of XML. There are a well know range of benefits of using JSON and I'm sure you know it. So I'll skip that.

To achieve what you want, you can use JSON.NET to serialize your DataTable into a JSON string very easily

public void Get()
{
    string jsonString = JsonConvert.SerializeObject(datatable, Formatting.Indented);

    string path = HttpContext.Current.Server.MapPath("~/App_Data/");

    //Generate a filename with your logic..
    string fileName = string.Concat(Guid.NewGuid().ToString(), ".json");

    //Create the full Path
    string fullPath = System.IO.Path.Combine(path, fileName);

    //Create the json file
    System.IO.File.WriteAllText(fullPath, jsonString);
}
jpgrassi
  • 5,482
  • 2
  • 36
  • 55
  • Good timing; I just NuGot JSON.NET and only had the JsonConvert.SerializeObject(dtAllResults); bit. – B. Clay Shannon-B. Crow Raven Dec 30 '15 at 23:04
  • You can go beyond that. If you know that your datatable structure will not change, thus providing a consistent json over time, you can create a class to map that json. When you access (read) the json file you can serialize it to the class and have a nicely strongly typed object, making your job much easier! – jpgrassi Dec 30 '15 at 23:07
  • This works like gangbusters; I wonder why, though, that even though the .json file *is* saved to the expected location, namely C:\Projects\PlatypusReports\PlatypusReports\App_Data, it is not visible in the Solution Explorer in the App_Data folder? If I right click the App_Data folder in the Solution Explorer and select "Open Folder in File Explorer" there it is, but in Solution Explorer, the App_Data folder is as bare as Mother Hubbard's cupboard. – B. Clay Shannon-B. Crow Raven Dec 30 '15 at 23:28
  • 1
    They are not listed in VS because they are not "included" in the project. To see them in VS, select the folder, then click on the Icon "Show all Files" The icon looks like two papers. Right next to it, there's a tool icon, which refers to the Property menu. – jpgrassi Dec 30 '15 at 23:31