locked
Add string to XML file RRS feed

  • Question

  • I have a string that is already formatted as an XML node and cannot figure out how to add this string as a line in the XML file. I am creating the XmlDocument and currently adding the nodes using XmlTextWriter, which works fine. Any assistance is appreciated.

    XmlDocument doc = new XmlDocument(); 
    static XmlTextWriter newXmlWriter = new XmlTextWriter(Path.GetFullPath(Directory.GetCurrentDirectory() + "/" + newFilename), Encoding.UTF8);
    newXmlWriter.WriteStartElement("ArchiveTask");
    newXmlWriter.WriteAttributeString("TaskName", shortname + "-ACG");
    newXmlWriter.WriteAttributeString("ArchiveLocation", archiveLocRoot + folder.Replace(root + "\\", ""));
    newXmlWriter.WriteAttributeString("ArchiveDirectory", folder);
    newXmlWriter.WriteAttributeString("Filter", "*.log|*.log.*|*.zip|*.html|*.htm*");
    newXmlWriter.WriteAttributeString("Recursive", "false");
    newXmlWriter.WriteAttributeString("LiveLogDays", "1");
    newXmlWriter.WriteAttributeString("ArchiveLogDays", "30");
    newXmlWriter.WriteEndElement();

    <?xml version="1.0" encoding="utf-8"?>
    <FileArchive>
      <TimeoutMinutesPerTask>120</TimeoutMinutesPerTask>
      <Tasks>
        <ArchiveTask TaskName="ACG-ACH" ArchiveLocation="E:\Logs\_Archive\ACH" ArchiveDirectory="D:\Q2\Logs\ACH" Filter="*.log|*.log.*|*.zip|*.html|*.htm*" Recursive="false" LiveLogDays="1" ArchiveLogDays="30" />
        <ArchiveTask TaskName="ACG-Application" ArchiveLocation="E:\Logs\_Archive\Application" ArchiveDirectory="D:\Q2\Logs\Application" Filter="*.log|*.log.*|*.zip|*.html|*.htm*" Recursive="false" LiveLogDays="1" ArchiveLogDays="30" />
        <!--The following line is an entire string that I want to add here-->
        <ArchiveTask TaskName="Test" ArchiveLocation="E:\Logs\_Archive\Test" ArchiveDirectory="D:\Q2\Logs\Application" Filter="*.log|*.log.*|*.zip|*.html|*.htm*" Recursive="false" LiveLogDays="1" ArchiveLogDays="30" />
      </Tasks>
     </FileArchive>

    Tuesday, September 30, 2014 5:14 PM

Answers

  • That shouldn't matter.  I did the same thing in my sample code.  I inserted a child element with some text.  Maybe I'm misunderstanding your issue but if you took the string you posted in your last response and inserted it using WriteRaw after your call to WriteEndElement in your previous post then you'd end up with 1 element that you rendered programmatically and the other that was represented by the string.

    writer.WriteStartElement("ArchiveTask");
    writer.WriteAttributeString("TaskName...");
    writer.WriteEndElement();
    writer.WriteRaw("<ArchiveTask TaskName=...");

    <ArchiveTask TaskName="" ... /> //From WriteStartElement..WriteEndElement
    <ArchiveTask TaskName="" ... /> //From WriteRaw

    The only time you'd run into an issue is if the string had the element that you already added via StartElement. If that is the case then somebody has to lose.  You either need to strip out the element definition and leave the attributes/content or not generate the  start element programmatically.  It depends upon your needs. 

    Tuesday, September 30, 2014 7:02 PM

All replies

  • You can use WriteString to write general strings out.  If you already have the XML content then look at using WriteRaw instead.

    var settings = new XmlWriterSettings() { CloseOutput = true, WriteEndDocumentOnClose = true };
    
    using (var writer = XmlWriter.Create(filename, settings))
    {
        writer.WriteStartElement("ArchiveTask");
        writer.WriteRaw("<Child>Text</Child>");
        writer.WriteEndElement();
    }

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Tuesday, September 30, 2014 5:48 PM
  • This is very good information, however there is still a problem in that the string already contains the start/end elements.

    <ArchiveTask TaskName="Test" ArchiveLocation="E:\Logs\_Archive\Test" ArchiveDirectory="D:\Q2\Logs\Application" Filter="*.log|*.log.*|*.zip|*.html|*.htm*" Recursive="false" LiveLogDays="1" ArchiveLogDays="30" />
    


    Tuesday, September 30, 2014 6:32 PM
  • That shouldn't matter.  I did the same thing in my sample code.  I inserted a child element with some text.  Maybe I'm misunderstanding your issue but if you took the string you posted in your last response and inserted it using WriteRaw after your call to WriteEndElement in your previous post then you'd end up with 1 element that you rendered programmatically and the other that was represented by the string.

    writer.WriteStartElement("ArchiveTask");
    writer.WriteAttributeString("TaskName...");
    writer.WriteEndElement();
    writer.WriteRaw("<ArchiveTask TaskName=...");

    <ArchiveTask TaskName="" ... /> //From WriteStartElement..WriteEndElement
    <ArchiveTask TaskName="" ... /> //From WriteRaw

    The only time you'd run into an issue is if the string had the element that you already added via StartElement. If that is the case then somebody has to lose.  You either need to strip out the element definition and leave the attributes/content or not generate the  start element programmatically.  It depends upon your needs. 

    Tuesday, September 30, 2014 7:02 PM