25

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?

Thanks in advance.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • 1
    The title is very misleading. It should really say something about file I/O in C# – Akrikos Feb 26 '09 at 15:12
  • Andrew why the rollback? – Gavin Miller Feb 26 '09 at 15:19
  • I think we should try to respect the original post as much as possible including the "Hi everyone" stuff as that was what the OP wrote. Nothing personal :) – Andrew Hare Feb 26 '09 at 15:21
  • Fair enough - When you rollback could you throw some rational in so as to prevent the rollback wars we've been seeing in the last couple of days! :D – Gavin Miller Feb 26 '09 at 15:23
  • Ah yes! That is excellent advice - I will do that in the future - thanks! – Andrew Hare Feb 26 '09 at 15:23
  • This question has nothing to do with XML, since OP already has the XML formatted into the string as desired. All they need to know is how to do file I/O in C#. For which there are already plenty of Q&As in stackoverflow. – ToolmakerSteve Oct 28 '15 at 21:49

4 Answers4

57

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.

Jerod Venema
  • 44,124
  • 5
  • 66
  • 109
26
File.WriteAllText("myFile.xml",myString);
BFree
  • 102,548
  • 21
  • 159
  • 201
  • 2
    +1 For saving overhead in converting the file to an XDocument just to get a convenient IO call. For serialization or non-namespaced Xml this approach is more efficient. – Jason Mar 30 '17 at 14:12
  • i want to use this but i dont know where will be store this file – Ramin Azali Aug 06 '22 at 09:50
0

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

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
-6

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

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]

Mike Bonnell
  • 180
  • 7
  • 8
    This is not a case of the right tool for the job. Your post is an example of the limitations of only knowing one tool and trying to force everything into it. If the OP is using C# for everything else, the introduction of VB.NET simply to save an XML string is ridiculous. – Ken White Feb 26 '09 at 18:39
  • 1
    No My post in an example of what an open mind can do by using projects of more than one language in the same solution. The VB language syntax is very easy to use for XML. I use both VB and C#. It is unclear how much "everything else" is done in C# by pragadheesh. Perhaps he explores alternatives. – Mike Bonnell Feb 26 '09 at 20:30
  • 9
    I still say introducing another language for the simple purpose of saving an XML string is ridiculous. Actually open your mind and think about what you're proposing: "Gee, I'll bring in a bulldozer, even though all I need is a trowel, to move this cup full of sand into the garden." – Ken White Feb 26 '09 at 20:44