用C#编写XML [英] Writing XML with C#

查看:154
本文介绍了用C#编写XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#有点生疏,我从来没有用它写过XML。如果我尝试写入元素以外的任何东西,那么我无法将XML写入文件。这里是我有的测试代码:

  var guiPath = txtGuiPath.Text; 
MessageBox.Show(Dumping File:+ guiPath);

try
{
var writer = new XmlTextWriter(client_settings.xml,null);
writer.WriteStartDocument();
writer.WriteComment(01/01/01生成的配置);
writer.WriteStartElement(Config);
writer.WriteStartElement(GuiPath);
writer.WriteString(guiPath);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
} catch(Exception ex){
MessageBox.Show(ex.Message);
}
MessageBox.Show(Finished Dumping);

如果guiPath为空,我得到以下XML:

 <?xml version =1.0?> 
<! - 在01/01/01生成的配置 - >
< Config>
< GuiPath />
< / Config>

但是如果在guiPath里有任何文本,那么没有任何东西被写入文件。我甚至可以删除client_settings.xml文件并反复激发这些代码,除非guiPath为空,否则XML文件将不会被生成。给WriteString()传递类似This is a test的东西也是一样。
$ b 更新

因为我试图写出一个系统路径,这似乎是问题所在。如果我去掉所有的反斜杠,它会正确的写出结果字符串,但是如果我把它传递给WriteString或者WriteCData,那么XML根本就不会写入。

更新2



原来我遇到这么多问题是因为XML文件是以guiPath设置的任何路径生成的,而不是该应用程序运行的目录(对我来说,它看起来好像根本没有被生成)。所以,如果我将guiPath设置为'C:\ Program Files \ externalApp \appName.exe',那么将XML文件保存为'C:\ Program Files \ externalApp \ client_settings.xml'而不是应用程序的启动文件夹。为什么,我不知道。我开始传递Application.StartupPath并附加文件名到现在它工作得很好。

感谢所有帮助!

解决方案

您可能需要检查System.Xml.Linq中的API。生成和编写XML是一种更灵活的方法。编写文档可能大致如下:

  XDocument document = new XDocument(); 
document.Add(new XComment(01/01/01生成的配置));
document.Add(new XElement(Config,new XElement(GuiPath,guiPath)));

// var xmlWriter = new XmlTextWriter(client_settings.xml,null);
// document.WriteTo(xmlWriter);

//感谢Barry Kelly指出XDocument.Save()
document.Save(client_settings.xml);


My C# is a bit rusty and I've never written XML with it before. I'm having trouble getting the XML to write to a file if I attempt to write anything other than elements. Here is the test code that I have:

var guiPath = txtGuiPath.Text;
MessageBox.Show("Dumping File: " + guiPath);

try
{
    var writer = new XmlTextWriter("client_settings.xml", null);
    writer.WriteStartDocument();
    writer.WriteComment("Config generated on 01/01/01");
    writer.WriteStartElement("Config");
    writer.WriteStartElement("GuiPath");
    writer.WriteString(guiPath);
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Close();
} catch (Exception ex) {
    MessageBox.Show(ex.Message);
}
MessageBox.Show("Finished Dumping");

If guiPath is blank I get the following XML:

<?xml version="1.0"?>
<!--Config generated on 01/01/01-->
<Config>
    <GuiPath />
</Config>

but if there is any text inside guiPath then nothing gets written to the file. I can even delete the client_settings.xml file and fire this code off over and over and the XML file never gets generated unless guiPath is empty. Passing something like "This is a test" to WriteString() works as well.

Update

Since I'm trying to write out a system path, that seems to be the problem. If I strip out all the backslashes it will write the resulting string correctly, but if I pass it to WriteString or WriteCData the XML will not write at all.

Update 2

Turns out that the reason I was having so many problems is because the XML file was being generated in whatever path guiPath was set to, not into the directory that the app was running from (so to me it looked like it wasn't being generated at all). So, if I had guiPath set to 'C:\Program Files\externalApp\appName.exe', it was saving the XML file as 'C:\ProgramFiles\externalApp\client_settings.xml' instead of in the startup folder for the app. Why, I don't know. I started passing Application.StartupPath and appended the filename to that and it works great now.

Thanks for all the help!

解决方案

You might want to examine the API in System.Xml.Linq. It's a bit of a more flexible approach to generating and writing XML. Writing your document might go roughly like this:

XDocument document = new XDocument();
document.Add(new XComment("Config generated on 01/01/01"));
document.Add(new XElement("Config", new XElement("GuiPath", guiPath)));

// var xmlWriter = new XmlTextWriter("client_settings.xml", null);
// document.WriteTo(xmlWriter);

// thanks to Barry Kelly for pointing out XDocument.Save()
document.Save("client_settings.xml");

这篇关于用C#编写XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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