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.

6
  • 1
    The title is very misleading. It should really say something about file I/O in C# Commented Feb 26, 2009 at 15:12
  • Andrew why the rollback? Commented Feb 26, 2009 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 :) Commented Feb 26, 2009 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 Commented Feb 26, 2009 at 15:23
  • Ah yes! That is excellent advice - I will do that in the future - thanks! Commented Feb 26, 2009 at 15:23

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

+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.
i want to use this but i dont know where will be store this file
0

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

Comments

-7

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]

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.