路径问题,怎样设置路径才能将创建的xml文件保存在项目中

guhonglangzi002 2010-09-08 11:57:00
怎样设置路径才能将创建的xml文件保存在项目中,代码中path的值设置的不对,应该怎样改啊?

代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace xml_test2
{
class Program
{
public static string path="D:";
public static void CreateDocument()
{
XDocument xdoc = new XDocument( new XDeclaration("1.0","utf-8","yes") , new XElement("Root","root") );
xdoc.Save(path);

}


public static void CreateCategories()
{
XElement root = new XElement("Categories",
new XElement ("Category",
new XElement ("CategoryID",Guid.NewGuid()),
new XElement("CategoryName", "Beverages")
),

new XElement ("Category",
new XElement ("CategoryID",Guid.NewGuid()),
new XElement("CategoryName", "Condiments")
),

new XElement ("Category",
new XElement ("CategoryID",Guid.NewGuid()),
new XElement("CategoryName", "Confections")
)

);
root.Save(path);
}


static void Main(string[] args)
{
Program.CreateDocument();
Program.CreateCategories();

}
}
}
...全文
344 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2010-09-09
  • 打赏
  • 举报
回复
public static string path=Application.ExecutePath
k0mmDu 2010-09-09
  • 打赏
  • 举报
回复
接楼上
需要屏蔽转义 应该是path=@".\name.xml"
k0mmDu 2010-09-09
  • 打赏
  • 举报
回复
path需要写文件名的么
要保存在可执行文件的目录下,直接path=".\name.xml"就好了么
guhonglangzi002 2010-09-09
  • 打赏
  • 举报
回复
顶下。该怎么设置呢?
萨拉嘿 2010-09-09
  • 打赏
  • 举报
回复
path是启动项目的debug文件夹下吧,使用相对路径试试
  • 打赏
  • 举报
回复
使用项目的启动路径:Application.StartupPath
需要屏蔽转义path=Application.StartupPath +"\\plugin"+@"\name.xml"
两种方式都可以屏蔽转义。

写在配置文件也可以,似乎程序更方便维护。
herty 2010-09-09
  • 打赏
  • 举报
回复
我的XML路径是写在 config 里面的。
<add key="XMLPath" value="ToolMenu.xml"></add>
然后在

System.Configuration.ConfigurationManager.AppSettings[XMLPath] 读出来的。

有哪位老大帮看看语音方面的问题

http://topic.csdn.net/u/20100909/10/efde4ac2-be3e-416d-a0d7-488df5d351a1.html?73491
jsjhj2008 2010-09-09
  • 打赏
  • 举报
回复
用Application.ExecutePath吧,是存在Debug里面的。路径是相对的
garfieldzf 2010-09-09
  • 打赏
  • 举报
回复
路径:Application.StarupPath;
shenrujun 2010-09-09
  • 打赏
  • 举报
回复
使用项目的启动路径:Application.StartupPath

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧