3

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

3
  • 1
    I wouldn't suggest using XmlWriter, to be honest - any reason you don't want to use LINQ to XML, which would make this trivial? Commented Aug 13, 2013 at 10:00
  • no reason :) please post as an answer. Commented Aug 13, 2013 at 10:01
  • see my answer below. Kinda wrote it longer than it took Jon to comment :) Commented Aug 13, 2013 at 10:08

5 Answers 5

4

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");
Sign up to request clarification or add additional context in comments.

1 Comment

I wouldn't use XmlDocument - XDocument is much simpler.
3

why not just LINQ to XML?

XElement ServerFp = new XElement("ServerFp",
    new XAttribute("Command", "Cached"),
    new XElement("CachedValue", "199.99")
);
Console.WriteLine(ServerFp.ToString());

outputting

<ServerFp Command="Cached">
  <CachedValue>199.99</CachedValue>
</ServerFp>

1 Comment

@JonSkeet thanks! Will update the answer for even more eunoia
2

Try this LINQ To XML

 XElement result = new XElement("ServerFp", new XAttribute("Command", "Cashed"),
                   new XElement("Cashed", new XAttribute("Value", "199.99"))
                   );

Output

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

Comments

1

this is how you can do it by using XmlWriter

writer.WriteStartDocument();
writer.WriteStartElement("ServerFp");
writer.WriteAttributeString("Command", "Cashed");
writer.WriteStartElement("Cashed");
writer.WriteAttributeString("Value", "199.99");
writer.WriteEndElement();
writer.WriteEndElement();

Or you can do the same using XDocument

XDocument doc = new XDocument(new XElement("ServerFp", new XAttribute("Command", "Cashed"), 
    new XElement("Cashed", new XAttribute("Value", "199.99"))));

doc.Save(filePath);

Comments

0

Try this (I am not sure but you can get an idea)

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

     writer.WriteEndElement(); // I think this will close your <Cashed Value="199.99"/>
     writer.WriteEndElement(); // I think this will close your 
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.