0

I have an xml file likes below.

<?xml version="1.0" encoding="utf-8" ?>
<Book>
  <Title>Title</Title>
  <Content>Content</Content>
</Book>

I want to write a new node after 'Content', I know how to use XMLDocument to do that, is there a way to use XMLTextWriter to do that?

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
James
  • 2,570
  • 7
  • 34
  • 57
  • 1
    please mention the language you are using i.e. C#, java etc. – Ramesh Soni Jun 27 '12 at 07:22
  • You want to modify the XML document, so why're you opposed to dealing with the XML document entity? XMLTextWriter is for writing an existing XML document entity to a file, not for modifying something. – penartur Jun 27 '12 at 07:30

1 Answers1

1

You will have to write the whole Xml document, i.e. all elements and attributes and attribute values by using the XmlTextWriter. After you've written the <Content> element, you can write your additional element.

Something like this:

writer.WriteStartDocument();
writer.WriteStartElement("Book");
writer.WriteStartElement("Title");
writer.WriteString("Title");
writer.WriteEndElement();
writer.WriteStartElement("Content");
writer.WriteString("Content");
writer.WriteEndElement();
// insert your new data here
writer.WriteEndElement();
writer.WriteEndDocument();
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114