如何将XML字符串写入文件?

时间:2022-04-04 22:09:42

I have a string and its value is:

我有一个字符串,它的值是:

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

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

如何使用c# 3.0将此字符串写入文件?

Thanks in advance.

提前谢谢。

4 个解决方案

#1


46  

Try this:

试试这个:

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

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

如果您的XML无效,那么负载将会失败。

#2


17  

File.WriteAllText("myFile.xml",myString);

#3


0  

You'll have to use CDATA section. More specifically, create a XmlCDataSection using XmlDocument.CreateCDataSection and supply your string as a parameter.

您必须使用CDATA部分。更具体地说,使用XmlDocument创建一个XmlCDataSection。CreateCDataSection并提供您的字符串作为参数。

#4


-3  

I know you said C# but have you tried VB.NET for XML Literals. Amazing stuff.

我知道你说的是c#,但是你试过VB吗?净XML文本。神奇的东西。

Public Class Program
    Public Shared Sub Main()
        Dim myKeyBoardStyle = "dvorak"

        Dim myXML As XElement = <ROOT>
                                qwerty
                                <altKeyboard><%= myKeyBoardStyle.ToUpper() %></altKeyboard>
                                    <SampleElement>adsf</SampleElement>
                                    <SampleElement2>The text of the sample element2</SampleElement2>
                                </ROOT>

        Console.WriteLine(myXML.ToString())

        myXML.Save(".\fileFromXElement.xml")
    End Sub
End Class

Notice the neat element which injects the result of code in into the output:

注意将代码的结果注入到输出中的整洁元素:

<?xml version="1.0" encoding="utf-8"?>
<ROOT>
                                qwerty
                                <altKeyboard>DVORAK</altKeyboard><SampleElement>adsf</SampleElement><SampleElement2>The text of the sample element2</SampleElement2></ROOT>

snip [removed opinions]

剪断(删除意见)

#1


46  

Try this:

试试这个:

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

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

如果您的XML无效,那么负载将会失败。

#2


17  

File.WriteAllText("myFile.xml",myString);

#3


0  

You'll have to use CDATA section. More specifically, create a XmlCDataSection using XmlDocument.CreateCDataSection and supply your string as a parameter.

您必须使用CDATA部分。更具体地说,使用XmlDocument创建一个XmlCDataSection。CreateCDataSection并提供您的字符串作为参数。

#4


-3  

I know you said C# but have you tried VB.NET for XML Literals. Amazing stuff.

我知道你说的是c#,但是你试过VB吗?净XML文本。神奇的东西。

Public Class Program
    Public Shared Sub Main()
        Dim myKeyBoardStyle = "dvorak"

        Dim myXML As XElement = <ROOT>
                                qwerty
                                <altKeyboard><%= myKeyBoardStyle.ToUpper() %></altKeyboard>
                                    <SampleElement>adsf</SampleElement>
                                    <SampleElement2>The text of the sample element2</SampleElement2>
                                </ROOT>

        Console.WriteLine(myXML.ToString())

        myXML.Save(".\fileFromXElement.xml")
    End Sub
End Class

Notice the neat element which injects the result of code in into the output:

注意将代码的结果注入到输出中的整洁元素:

<?xml version="1.0" encoding="utf-8"?>
<ROOT>
                                qwerty
                                <altKeyboard>DVORAK</altKeyboard><SampleElement>adsf</SampleElement><SampleElement2>The text of the sample element2</SampleElement2></ROOT>

snip [removed opinions]

剪断(删除意见)