如何将这个字符串保存到XML文件中?

时间:2021-11-23 01:04:26

I have this string variable:

我有这个字符串变量:

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>";

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

如何将该字符串保存到驱动器c中的XML文件中?使用c#。

6 个解决方案

#1


52  

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

它是XML的事实基本上是无关的。使用file . writealltext:

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

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:

注意,您还可以指定编码,默认为UTF-8。举个例子,如果你想用普通ASCII码写一个文件:

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

#2


7  

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(yourXMLString);
xdoc.Save("myfilename.xml");

#3


7  

If you don't need to do any processing on the string (with an XML library, for example), you could just do:

如果您不需要对字符串进行任何处理(例如使用XML库),您可以这样做:

File.WriteAllText(@"c:\myXml.xml", xml);

#4


6  

System.IO.File.WriteAllText("filename.xml", xml );

#5


2  

You can do this:

你可以这样做:

string path = @"C:\testfolder\testfile.txt";

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
{    
    file.Write(text);  
}

You can also do this after you've created an XML Document, but it is slower:

在创建了XML文档之后,您还可以这样做,但速度较慢:

xdoc.Save

#6


1  

If you want to save the string as-is without performing any check on whether it's well-formed or valid, then as has been answered above, use System.IO.File.WriteAllText("C:\myfilename.xml", xml );

如果您想要保存该字符串,而不需要检查它是否格式良好或是否有效,那么如上所述,使用System.IO.File.WriteAllText(“C:\myfilename”。xml,xml);

As has also been noted, this defaults to saving the file as UTF-8, but you can specify encoding as Jon Skeet mentioned.

如前所述,默认情况下将文件保存为UTF-8,但是可以指定如Jon Skeet所提到的编码。

I'd recommend adding an XML declaration to the string, e.g.,

我建议在字符串中添加一个XML声明,例如,

<?xml version="1.0" encoding="UTF-8"?>

and ensuring the encoding in the declaration matches that in the WriteAllText method. It's likely to save a fair amount of hassle at later date, judging by the frequency of XML encoding questions on *.

并确保声明中的编码与WriteAllText方法中的编码匹配。从*的XML编码问题的频率来看,它很可能在以后节省大量的麻烦。

If you want to ensure the XML is well-formed and/or valid, then you will need to use an XML parser on it first, such as XDocument doc = XDocument.Parse(str); That method is also overridden if you want to preserve whitespace: XDocument.Parse(str, LoadOptions.PreserveWhitespace)

如果您希望确保XML格式良好和/或有效,则需要首先在其上使用XML解析器,例如XDocument doc = XDocument. parse (str);如果希望保留空格:XDocument,也可以重写该方法。LoadOptions.PreserveWhitespace Parse(str)

You can then perform validation on it http://msdn.microsoft.com/en-us/library/bb340331.aspx

然后可以对它执行验证http://msdn.microsoft.com/en-us/library/bb340331.aspx

before saving to file: doc.Save("C:\myfilename.xml");

保存文件前:doc.Save(“C:\myfilename.xml”);

#1


52  

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

它是XML的事实基本上是无关的。使用file . writealltext:

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

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:

注意,您还可以指定编码,默认为UTF-8。举个例子,如果你想用普通ASCII码写一个文件:

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

#2


7  

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(yourXMLString);
xdoc.Save("myfilename.xml");

#3


7  

If you don't need to do any processing on the string (with an XML library, for example), you could just do:

如果您不需要对字符串进行任何处理(例如使用XML库),您可以这样做:

File.WriteAllText(@"c:\myXml.xml", xml);

#4


6  

System.IO.File.WriteAllText("filename.xml", xml );

#5


2  

You can do this:

你可以这样做:

string path = @"C:\testfolder\testfile.txt";

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
{    
    file.Write(text);  
}

You can also do this after you've created an XML Document, but it is slower:

在创建了XML文档之后,您还可以这样做,但速度较慢:

xdoc.Save

#6


1  

If you want to save the string as-is without performing any check on whether it's well-formed or valid, then as has been answered above, use System.IO.File.WriteAllText("C:\myfilename.xml", xml );

如果您想要保存该字符串,而不需要检查它是否格式良好或是否有效,那么如上所述,使用System.IO.File.WriteAllText(“C:\myfilename”。xml,xml);

As has also been noted, this defaults to saving the file as UTF-8, but you can specify encoding as Jon Skeet mentioned.

如前所述,默认情况下将文件保存为UTF-8,但是可以指定如Jon Skeet所提到的编码。

I'd recommend adding an XML declaration to the string, e.g.,

我建议在字符串中添加一个XML声明,例如,

<?xml version="1.0" encoding="UTF-8"?>

and ensuring the encoding in the declaration matches that in the WriteAllText method. It's likely to save a fair amount of hassle at later date, judging by the frequency of XML encoding questions on *.

并确保声明中的编码与WriteAllText方法中的编码匹配。从*的XML编码问题的频率来看,它很可能在以后节省大量的麻烦。

If you want to ensure the XML is well-formed and/or valid, then you will need to use an XML parser on it first, such as XDocument doc = XDocument.Parse(str); That method is also overridden if you want to preserve whitespace: XDocument.Parse(str, LoadOptions.PreserveWhitespace)

如果您希望确保XML格式良好和/或有效,则需要首先在其上使用XML解析器,例如XDocument doc = XDocument. parse (str);如果希望保留空格:XDocument,也可以重写该方法。LoadOptions.PreserveWhitespace Parse(str)

You can then perform validation on it http://msdn.microsoft.com/en-us/library/bb340331.aspx

然后可以对它执行验证http://msdn.microsoft.com/en-us/library/bb340331.aspx

before saving to file: doc.Save("C:\myfilename.xml");

保存文件前:doc.Save(“C:\myfilename.xml”);