使用c#创建一个XML文件

时间:2021-05-09 01:23:28

Please suggest me a method to save an XML file to the current installation directory of the application created using C#.

请建议一种方法将XML文件保存到使用c#创建的应用程序的当前安装目录。

4 个解决方案

#1


8  

  1. Create an XML file: The easiest way is to create and populate an XmlDocument or XDocument object.

    创建XML文件:最简单的方法是创建和填充XmlDocument或XDocument对象。

  2. Save to the install directory: use

    保存到安装目录:使用。

   string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);  
   string file = System.IO.Path.Combine(pathm, "myfile.xml");

But you do know that the application's folder isn't the best place to store a file, right?

但是您知道应用程序的文件夹不是存储文件的最佳位置,对吗?

Edit:

Some comments mention Isolated Storage, but that is overkill. The best way to store data is to use the appropriate DataPath. That is different under various versions of Windows, but this always works:

有些评论提到了独立存储,但这有点过头了。存储数据的最佳方式是使用适当的数据处理。在不同版本的Windows下,这是不一样的,但这总是有效的:

string path = 
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

There are a few other values in the Environment.SpecialFolder enum, take a look.

环境中还有一些其他的值。SpecialFolder枚举,看看。

#2


7  

Using XDocument and LINQ:

使用XDocument和LINQ:

XDocument myXml = new XDocument(new XElement("Node 1", new XElement("Node 2")));
myXml.Save(Directory.GetCurrentDirectory() + "/myXML.xml");

or

XDocument myXml = new XDocument(new XElement("Node 1", new XElement("Node 2")));
myXml.Save(Path.GetDirectoryName(Application.ExecutablePath) + "/myXML.xml");

#3


0  

StringBuilders and HttpUtility.HtmlEncode usually work quite well.

StringBuilders HttpUtility。HtmlEncode通常工作得很好。

#4


0  

And, after you've tried everything and are still scratching your head, wondering why it isn't working...

而且,当你试过了所有的方法,还在挠头,想知道为什么它不管用……

  1. Make sure the folder you want to save to ALLOWS Writing by the account the Web service is running under or the user (depending on how you have security configured); this should be done at the NTFS level. Previous suggestions said that it would be bad to write in the application folder. No, it wouldn't be bad, it would be incredibly stupid to write there, not just bad. So, pick somewhere else to save the data. Ideally on another physical volume, or better still: post the XML file to another application on another server who's only lot in life is saving this data.
  2. 确保要保存的文件夹允许由Web服务运行的帐户或用户(取决于您如何配置安全性)编写;这应该在NTFS级完成。先前的建议说,在应用程序文件夹中编写是不好的。不,这不是坏事,在那里写是非常愚蠢的,不仅仅是坏的。所以,选择其他地方保存数据。理想情况下,在另一个物理卷上,或者更好的方法是:将XML文件放到另一个服务器上的另一个应用程序中,而在这个服务器上,只有很多人在保存这些数据。
  3. Assuming you know how to create an XML document in C# (strings ain't it - use the proper methods for building an XmlDocument - like by using elements and nodes and attributes), you can use the .Save method
  4. 假设您知道如何在c#中创建XML文档(字符串不是它——使用合适的方法构建XmlDocument——比如使用元素、节点和属性),您可以使用.Save方法
  5. Why not use strings??? Because you'll spend more time debugging "well-formed XML" errors than you will solving your actual problem. People are lousy at making well-formed XML. Machines are pretty good at it.
  6. 为什么不使用字符串? ? ?因为调试“格式良好的XML”错误的时间比解决实际问题的时间要长。人们不擅长制作格式良好的XML。机器很擅长。

#1


8  

  1. Create an XML file: The easiest way is to create and populate an XmlDocument or XDocument object.

    创建XML文件:最简单的方法是创建和填充XmlDocument或XDocument对象。

  2. Save to the install directory: use

    保存到安装目录:使用。

   string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);  
   string file = System.IO.Path.Combine(pathm, "myfile.xml");

But you do know that the application's folder isn't the best place to store a file, right?

但是您知道应用程序的文件夹不是存储文件的最佳位置,对吗?

Edit:

Some comments mention Isolated Storage, but that is overkill. The best way to store data is to use the appropriate DataPath. That is different under various versions of Windows, but this always works:

有些评论提到了独立存储,但这有点过头了。存储数据的最佳方式是使用适当的数据处理。在不同版本的Windows下,这是不一样的,但这总是有效的:

string path = 
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

There are a few other values in the Environment.SpecialFolder enum, take a look.

环境中还有一些其他的值。SpecialFolder枚举,看看。

#2


7  

Using XDocument and LINQ:

使用XDocument和LINQ:

XDocument myXml = new XDocument(new XElement("Node 1", new XElement("Node 2")));
myXml.Save(Directory.GetCurrentDirectory() + "/myXML.xml");

or

XDocument myXml = new XDocument(new XElement("Node 1", new XElement("Node 2")));
myXml.Save(Path.GetDirectoryName(Application.ExecutablePath) + "/myXML.xml");

#3


0  

StringBuilders and HttpUtility.HtmlEncode usually work quite well.

StringBuilders HttpUtility。HtmlEncode通常工作得很好。

#4


0  

And, after you've tried everything and are still scratching your head, wondering why it isn't working...

而且,当你试过了所有的方法,还在挠头,想知道为什么它不管用……

  1. Make sure the folder you want to save to ALLOWS Writing by the account the Web service is running under or the user (depending on how you have security configured); this should be done at the NTFS level. Previous suggestions said that it would be bad to write in the application folder. No, it wouldn't be bad, it would be incredibly stupid to write there, not just bad. So, pick somewhere else to save the data. Ideally on another physical volume, or better still: post the XML file to another application on another server who's only lot in life is saving this data.
  2. 确保要保存的文件夹允许由Web服务运行的帐户或用户(取决于您如何配置安全性)编写;这应该在NTFS级完成。先前的建议说,在应用程序文件夹中编写是不好的。不,这不是坏事,在那里写是非常愚蠢的,不仅仅是坏的。所以,选择其他地方保存数据。理想情况下,在另一个物理卷上,或者更好的方法是:将XML文件放到另一个服务器上的另一个应用程序中,而在这个服务器上,只有很多人在保存这些数据。
  3. Assuming you know how to create an XML document in C# (strings ain't it - use the proper methods for building an XmlDocument - like by using elements and nodes and attributes), you can use the .Save method
  4. 假设您知道如何在c#中创建XML文档(字符串不是它——使用合适的方法构建XmlDocument——比如使用元素、节点和属性),您可以使用.Save方法
  5. Why not use strings??? Because you'll spend more time debugging "well-formed XML" errors than you will solving your actual problem. People are lousy at making well-formed XML. Machines are pretty good at it.
  6. 为什么不使用字符串? ? ?因为调试“格式良好的XML”错误的时间比解决实际问题的时间要长。人们不擅长制作格式良好的XML。机器很擅长。