我怎样写一个XML字符串到一个文件? [英] How do I write an XML string to a file?

查看:180
本文介绍了我怎样写一个XML字符串到一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其值是:

I have a string and its value is:

<ROOT>
    qwerty
    <SampleElement>adsf</SampleElement> 
    <SampleElement2>The text of the sample element2</SampleElement2> 
</ROOT>



我如何使用C#3.0这个字符串写一个文件?

How can I write this string to a file using C# 3.0?

在此先感谢

推荐答案

试试这个:

string s = "<xml><foo></foo></xml>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(s);
xdoc.Save("myfilename.xml");



有额外的好处,如果你的XML是无效的负载将失败。

Has the added benefit that the load will fail if your XML is invalid.

这篇关于我怎样写一个XML字符串到一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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