在c#.net中创建Xml文件时出错 [英] Error while creating Xml File in c#.net

查看:62
本文介绍了在c#.net中创建Xml文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正在尝试在c#.net Windows应用程序中创建xml文件.我正在使用以下代码来执行此操作

Hello
I am trying to create xml file in c#.net windows application.I am using following code to do this

writer = XmlWriter.Create((Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) + "\\PurchaseBill.xml");



但这会给类似
的错误
不支持URI格式."

请帮助


在此先感谢



But It Will give error like

"URI formats are not supported."

Pls Help


Thanks In Advance

推荐答案

CodeBase 属性返回一个Uri,您需要获取绝对路径才能使用XmlWriter;

The CodeBase property returns a Uri, you need to get the absolute path out for it to work with the XmlWriter;

uri = new Uri(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string path = uri.AbsolutePath;
Directory.CreateDirectory(path);
XmlWriter writer = XmlWriter.Create(String.Format("{0}\\PurchaseBill.xml", path));



希望这会有所帮助,
Fredrik



Hope this helps,
Fredrik


尝试将您的代码更改为
Try changing your code to
string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\PurchaseBill.xml";
writer = XmlWriter.Create(path);


并使用调试器来验证路径的格式正确.


and use the debugger to verify that your path is correctly formatted.


这篇关于在c#.net中创建Xml文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆