Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create xml file, basics

Tags:

c#

.net

I want to create xml document with following structure

<ServerFp Command="Cashed">
    <Cashed Value="199.99"/>
</ServerFp>

So I tried like this :

XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
            using (var writer = XmlWriter.Create(filename, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("ServerFp");
                writer.WriteAttributeString("Command", "Cashed");

            }

Is this good so far and how to end this file? with node <Cashed Value="199.99"/>

like image 709
panjo Avatar asked Aug 13 '26 16:08

panjo


1 Answers

I would try doing it like this:

create a new XmlDocument:

XmlDocument doc = new XmlDocument();

create your nodes you want to insert

XmlNode node1 = doc.CreateElement("node1")

append your element

doc.AppendChild(node1 );

save the document

doc.Save("result.xml");
like image 87
Dr. Pee Avatar answered Aug 16 '26 07:08

Dr. Pee