Search code examples
c#xmlxmlwriter

Using XMLWriter to create a custom header in C#


I'm fairly new to XML and very new to using the XMLWriter object. I've been successful in using it to write a "Well Formed" XML file, but after many failed attempts to create the needed header, below, I've decided to come here for some insight.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE IDMS-XML SYSTEM "http://eclipseinc.com/dtd/IDMS-XML.dtd"> 
<IDMS-XML>
....

Here's the beginning of my code (very standard):

using (XmlWriter xmlWriter = XmlWriter.Create("SendXML.xml"))
{
    xmlWriter.WriteStartDocument();
    ....

I've tried using things like xmlWriter.WriteString() to force it in, but that has been unsuccessful for me. Thanks for any and all insight.


Solution

  • In order to write to the xml file you need to create the XmlTextWriter then create a node to make a header. Hope this helps you out a bit.

    XmlTextWriter writer = new XmlTextWriter("filename",System.Text.Encoding.UTF8);                  writer.WriteStartDocument(True)
    
        writer.WriteStartElement("Start Element Name")
        createNode("NodeName", writer)
        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()
    
    
    public void createnode(String nodename, XmlTextWriter writer)
    {
        writer.WriteStartElement("Name Here")
        writer.WriteString(nodename)
        writer.WriteEndElement()
    }