Search code examples
c#xmlxmltextwriter

How to wrtie a XML License Line(ended with a forward slash '/') in C#?


I want to write a XML file as below:

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <License licenseId="" licensePath="" />

Some piece of my code attached here

    // Create a new file in D:\\ and set the encoding to UTF-8
    XmlTextWriter textWriter = new XmlTextWriter("D:\\books.xml", System.Text.Encoding.UTF8);

    // Format automatically
    textWriter.Formatting = Formatting.Indented;

    // Opens the document
    textWriter.WriteStartDocument();

    // Write the namespace declaration.
    textWriter.WriteStartElement("books", null);
    // Write the genre attribute.
    textWriter.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
    textWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");

And now I need to write the License Line below in C#

<License licenseId="" licensePath="" />

But I don't know how to move on for I found the Line ended with the forward slash / .Thank you.


Solution

  • Calling the WriteEndElement method will automatically take care of adding the forwards slash.