Question

I have a method which creates an xml file and this method is frequently called from other classes. I have sent three parameters to this method and while writing the xml file I need to append in the xml file with those three parameters. Here's my code snippet

XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        XmlWriter writer = XmlWriter.Create(@"E:\\log.xml", settings);

        writer.WriteStartDocument();
        writer.WriteStartElement("Log");

        writer.WriteStartElement("Tests");
        writer.WriteStartElement("Test");
        writer.WriteAttributeString("Test", message);
        writer.WriteElementString("DateAndTime", time);
        writer.WriteElementString("Result", test);
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();

I get his output :

<?xml version="1.0" encoding="utf-8"?>
<Log>
  <Tests>
    <Test Test="LoginToAcrm">
      <DateAndTime>2014-20-28 06:20:40</DateAndTime>
      <Result>Passed</Result>
    </Test>
  </Tests>
</Log>

Can anyone please tell me what should I change in my code so that when I call the method again with these three parameters it will append to this xml file with the same format, not overwrite the previous file. Something will be like this:

<?xml version="1.0" encoding="utf-8"?>
<Log>
  <Tests>
    <Test Test="LoginToAcrm">
      <DateAndTime>2014-20-28 06:20:40</DateAndTime>
      <Result>Passed</Result>
    </Test>
  </Tests>
  <Tests>
    <Test Test="LoginToProjectWithError">
      <DateAndTime>2014-09-28 05:10:45</DateAndTime>
      <Result>Failed</Result>
    </Test>
  </Tests>
  <Tests>
    <Test Test="LoginToProjectWithBlank">
      <DateAndTime>2014-09-28 05:12:13</DateAndTime>
      <Result>Passed</Result>
    </Test>
  </Tests>
</Log>
Was it helpful?

Solution

I used some your code, use StringBuilder to create XmlWriter and finally write that string using StreamWriter.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

settings.OmitXmlDeclaration = true;
StringBuilder sb = new StringBuilder();

XmlWriter writer = XmlWriter.Create(sb, settings);

writer.WriteStartDocument();

writer.WriteStartElement("Tests");
writer.WriteStartElement("Test");
writer.WriteAttributeString("Test", message);
writer.WriteElementString("DateAndTime", time);
writer.WriteElementString("Result", test);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\\log.xml", true))
{
    file.Write(sb.ToString());
}

OTHER TIPS

You can achieve this by passing List of strings as arguments to the function. And for your help i did the coding of the complete function. Let me know if this work out for you. Check this out

void xmlwriter(List<string> message, List<string> time, List<string> test)
    {
    XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        XmlWriter writer = XmlWriter.Create(@"E:\\log.xml", settings);

        writer.WriteStartDocument();

        writer.WriteStartElement("Tests");
        string[] arrayMessage = message.ToArray();
        string[] arrayTime = time.ToArray();
        string[] arrayTest = test.ToArray();
        for (int i = 0; i < arrayMessage.Length; i++)
        {
            writer.WriteStartElement("Test");
            writer.WriteAttributeString("Test", arrayMessage[i]);
            writer.WriteElementString("DateAndTime", arrayTime[i]);
            writer.WriteElementString("Result", arrayTest[i]);
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();
    }

Note: You may have to add all the values of the parameters to the List before you call the function.

Your desired result isn't a well-formed XML document, since an XML document may only have a single root element.

However, if this is what you want (i.e. rather than an XML document, you want a text document containing XML fragments), you could try the following:

  • Open the existing file as a FileStream
  • Seek to the end of file
  • Pass the FileStream to your XmlWriter constructor
  • Append the elements you want
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top