如何保存这个字符串转换成XML文件? [英] How to save this string into XML file?

查看:114
本文介绍了如何保存这个字符串转换成XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串变量:

string xml = @"<Contacts> 
    <Contact> 
    <Name>Patrick Hines</Name> 
    <Phone Type=""Home"">206-555-0144</Phone> 
    <Phone Type=""Work"">425-555-0145</Phone> 
    <Phone Type=""Mobile"">332-899-5678</Phone> 
    <Address> 
        <Street1>123 Main St</Street1> 
        <City>Mercer Island</City> 
        <State>WA</State> 
        <Postal>68042</Postal> 
    </Address> 
    </Contact> 
    <Contact> 
    <Name>Dorothy Lee</Name> 
    <Phone Type=""Home"">910-555-1212</Phone> 
    <Phone Type=""Work"">336-555-0123</Phone> 
    <Phone Type=""Mobile"">336-555-0005</Phone> 
    <Address> 
        <Street1>16 Friar Duck Ln</Street1> 
        <City>Greensboro</City> 
        <State>NC</State> 
        <Postal>27410</Postal> 
    </Address> 
    </Contact>
</Contacts>";



我怎样才能拯救这个字符串转换成XML文件在我的C盘?使用C#。

How can I save this string into an XML file in my drive c? Using c#.

推荐答案

这是XML基本上是无关紧要的事实,你可以任意的文本保存到一个文件很简单地用 File.WriteAllText

The fact that it's XML is basically irrelevant. You can save any text to a file very simply with File.WriteAllText:

File.WriteAllText("foo.xml", xml);

请注意,您还可以指定编码,默认为UTF-8。因此,例如,如果你想要写一个文件纯ASCII:

Note that you can also specify the encoding, which defaults to UTF-8. So for example, if you want to write a file in plain ASCII:

File.WriteAllText("foo.xml", xml, Encoding.ASCII);

这篇关于如何保存这个字符串转换成XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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