Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending an existing XML file with XmlWriter

I've used the following code to create an XML file:

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.NewLineOnAttributes = true;
using (XmlWriter xmlWriter = XmlWriter.Create("Test.xml", xmlWriterSettings))
{
   xmlWriter.WriteStartDocument();
   xmlWriter.WriteStartElement("School");
   xmlWriter.WriteEndElement();
   xmlWriter.WriteEndDocument();
   xmlWriter.Close();
 }

I need to insert nodes dynamically creating the following structure:

<?xml version="1.0" encoding="utf-8"?>
<School />
   <Student>
      <FirstName>David</FirstName>
      <LastName>Smith</LastName>
   </Student>
   ...
   <Teacher>
      <FirstName>David</FirstName>
      <LastName>Smith</LastName>
   </Teacher>
   ...
</School>

How can I do it? The values of "FirstName" and "LastName" should be read from the keyboard and the values ​​can be entered at any time, of course under existing.

like image 749
user3105160 Avatar asked Jan 04 '14 15:01

user3105160


People also ask

How to add element in existing xml using c#?

To create each element you should append the namespace element flp in order to match with other elements, to add namespace to each Element use the following code. XmlElement D100 = doc. CreateElement("flp","D100","http://www.w3.org/2001/XMLSchema"); D100. SetAttribute("Number", "2"); XmlElement Code = doc.

How do I edit an existing XML file?

From the Project menu, select Add New Item. Select XML File from the Templates pane. Enter the filename in the Name field and press Add. The XML file is added to the project and opens in the XML editor.


1 Answers

I have a suggestion for the next time:

string nameFile = "Test.xml";
bool newFile = false;


if (!File.Exists(nameFile))
{
    newFile = true;
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
    xmlWriterSettings.Indent = true;
    xmlWriterSettings.NewLineOnAttributes = true;

    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("School");

    xmlWriter = XmlWriter.Create("Test.xml", xmlWriterSettings))
}
else
{
    doc = new XmlDocument();
    doc.Load(nameFile);

    // Create a XPathNavigator
    // You can go where you want to add
    // In this case it is just after last child of the roor
    XPathNavigator navigator = doc.CreateNavigator();     

    navigator.MoveToChild("School", "");
    xmlWriter = navigator.AppendChild();
}

// From here you can work only with xmlWriter,
// One will point on a file and the other on the stream of xmlDocument
// So you will need to save the document in the second choise

xmlWriter.WriteStartElement("Student");
xmlWriter.WriteElementString("FirstName", firstName);
xmlWriter.WriteElementString("LastName", lastName);
xmlWriter.WriteEndElement();


// End document / close or save. 
if (newFile)
    xmlWriter.WriteEndDocument();

xmlWriter.Close();

if (!newFile)
    doc.Save(nameFile);

It should work. :)

like image 186
Al3x_M Avatar answered Oct 24 '22 06:10

Al3x_M