How do I write an XML string to a file?
Asked Answered
E

4

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.

Embus answered 26/2, 2009 at 14:59 Comment(6)
The title is very misleading. It should really say something about file I/O in C#Amoroso
Andrew why the rollback?Altitude
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 :)Wotton
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! :DAltitude
Ah yes! That is excellent advice - I will do that in the future - thanks!Wotton
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.Nephridium
I
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.

Islean answered 26/2, 2009 at 15:7 Comment(0)
R
27
File.WriteAllText("myFile.xml",myString);
Rasp answered 26/2, 2009 at 15:15 Comment(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.Vernissage
i want to use this but i dont know where will be store this fileUpdate
M
0

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

Menhaden answered 26/2, 2009 at 15:1 Comment(0)
S
-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]

Substance answered 26/2, 2009 at 15:28 Comment(3)
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.Misdoing
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.Substance
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."Misdoing

© 2022 - 2024 — McMap. All rights reserved.