How can I create an empty XmlElementString with XmlWriter

I am trying to dump out a collection of values from an editor I am writing. There are some elements that I need to write out, even though they are empty. Whenever I try something like...

writer.WriteElementString("variable", ""); or writer.WriteElementString("variable", string.Empty);

I get <variable /> instead of <variable></variable>

I tried a WriteStartElement("variable") followed by a WriteEndElement but that gave me...

<variable>
</variable>

Does the line separation make any difference to having it all on one line




Answer this question

How can I create an empty XmlElementString with XmlWriter

  • b6s

    Yes, unfortunately, there are still a lot of non-xml readers trying to parse xml files out there...

    Anyway, if you're still struggling with this, here's what worked for me:

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent =
    true;
    using (XmlWriter tw = XmlTextWriter.Create("c:\\test.xml", filename, settings))
    {
       
    tw.WriteStartDocument();
       
    tw.WriteStartElement(
    "document");
       
    tw.WriteString(
    string.Empty);
       
    tw.WriteEndElement();
       
    tw.WriteEndDocument();
    }

    Andrej



  • pst_grant

    i can see you already have that in the code and it should work

    tw.WriteStartElement("variable", String.EMpty);


  • sic0198

    Try something like this

    XmlTextWriter tw = new XmlTextWriter(filename, null);

    tw.Formatting = Formatting.Indented;

    tw.WriteStartDocument();

    tw.WriteStartElement(""); // This should make an empty element...


  • ajay_s

    LeeC22 wrote:
    I get <variable /> instead of <variable></variable>

    Hi, just wondering what's wrong with having "<variable />". It's just the syntax, semantics is the same...

    Andrej



  • bitbonk

    Andrej Tozon wrote:

    LeeC22 wrote:
    I get <variable /> instead of <variable></variable>

    Hi, just wondering what's wrong with having "<variable />". It's just the syntax, semantics is the same...

    Andrej

    It needs the <variable></variable> as the data could potentially get parsed by a non-XML reader so it needs to specify data in a specific way.



  • SoulSolutions

    Hi Harsimrat,

    It doesn't need to be totally empty, the variable itself may be empty (or null) but it still needs to generate the "variable" element.

    I.e.. <variable></variable>

    whereas normally it would be <variable>Value</variable> if there was something in it.



  • ropley

    For what it's worth, this probably is about XmlTextWriter.Create... I replaced it with the new XmlTextWriter() in the above code and got <document />.

    Andrej



  • LSS

    Andrej Tozon wrote:

    Yes, unfortunately, there are still a lot of non-xml readers trying to parse xml files out there...

    Anyway, if you're still struggling with this, here's what worked for me:

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent =
    true;
    using (XmlWriter tw = XmlTextWriter.Create("c:\\test.xml", filename, settings))
    {
    tw.WriteStartDocument();
    tw.WriteStartElement(
    "document");
    tw.WriteString(
    string.Empty);
    tw.WriteEndElement();
    tw.WriteEndDocument();
    }

    Andrej

    Strangely, that piece of code works fine, when I incorporate that into my editor, I get the same <variable /> output.

    The loop I have is basically this...

    foreach (String mapTops in gameLevel.sMapTop)
    {

    if (mapTops == "")
    {
    writer.WriteStartElement(
    "mapTop");
    writer.WriteString(
    string.Empty);
    writer.WriteEndElement();
    }
    else
    {
    writer.WriteElementString(
    "mapTop", mapTops);
    }

    }

    mapTops is an ArrayList()

    The only other difference is I don't use the XmlTextWriter.Create method but even when I used that instead of my current method, it still failed...

    I might have to leave this on the backburner for now, it's becoming an unnecessary distraction. There's obviously something being written beforehand that is causing this change in output, I just don't know enough about XML to know what it is.

    Thanks for the help though guys, much appreciated.



  • How can I create an empty XmlElementString with XmlWriter