1

I'm creating a program using C# (in ASP.NET environment) that makes an XML file. It uses XmlWriter class for writing, but there's one thing I'm not sure how to do.

Say, to write a string I do:

xmlWriter.WriteElementString("c1", "name");

which becomes:

< c1 >name< /c1 >

That is great, until I try to write an empty string:

xmlWriter.WriteElementString("c1", "");

which becomes:

< c1/ >

But how do you format it to be?

< c1 >< /c1 >

ahmd0
  • 16,633
  • 33
  • 137
  • 233
  • 2
    possible duplicate of [C#: XmlTextWriter.WriteElementString fails on empty strings?](http://stackoverflow.com/questions/1176202/c-xmltextwriter-writeelementstring-fails-on-empty-strings) – nemesv Mar 17 '12 at 23:12
  • Yes, it is a duplicate and that link answers my question. Thank you. – ahmd0 Mar 17 '12 at 23:22
  • I take it back. It actually doesn't fix it. I still can't force it to use closing tag.... if anyone knows why, please reply... – ahmd0 Mar 17 '12 at 23:47

3 Answers3

3
using (XmlWriter writer = new XmlTextWriter(stream)
{
    writer.WriteStartElement("c1") 
    writer.WriteString("") 
    writer.WriteFullEndElement()
}

EDIT: Here's testable code. It doesn't matter how you create the XmlWriter

public static void Main()
{
    using (StringWriter sw = new StringWriter())
    {
        using (XmlWriter xw = XmlWriter.Create(sw))
        {
            xw.WriteStartElement("c1");
            xw.WriteString(string.Empty);
            xw.WriteFullEndElement();
        }

        Console.Write(sw.ToString());  // Prints <c1></c1>
    }
}

What implementation of XmlWriter are you using?

Tung
  • 5,334
  • 1
  • 34
  • 41
  • Is it supposed to be XmlTextWriter, or just XmlWriter? Because I can't seem to make it work with jusr XmlWriter... – ahmd0 Mar 17 '12 at 23:35
  • XmlWriter is an abstract class. What implementation of XmlWriter are you using? You can also use `using (XmlWriter writer = XmlWriter.Create(stream))`. I doubt that it matters which writer you are using. The focus should be on explicitly writing a start element, and a full end element. – Tung Mar 17 '12 at 23:47
2

Both contructs (<c1/> and <c1></c1>) are identical from XML point of view, so it is very likely that compliant reader will not let you distinguish between them. If you need to have special "null" value consider using xsi:nil ( http://w3.org/TR/xmlschema-1/#xsi_nil ) which is designed for this.

<c1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

< c1/ > is proper for an empty element. Much like how you would use < br/ > in html for a new line.

If you do not like this syntax, although it is correct, you could manually parse all tags containing /> to be broken into <> </> manually. Otherwise, perhaps you should consider using xmlWriter.WriteElementString("c1", " ");

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Yes, I know that. For my purpose two make a difference. < c1/ > is treated as null and < c1 > c1 > - as an empty string. – ahmd0 Mar 17 '12 at 23:15
  • 1
    @ahmd0, Both contructs are identical from XML point of view, so it is very likely that compliant reader will not let you distinguish between them. If you need to have special "null" value consider using xsi:nil (http://www.w3.org/TR/xmlschema-1/#xsi_nil) which is designed for this. – Alexei Levenkov Mar 18 '12 at 00:36
  • @Alexei Levenkov why don't you post it as a separate answer. This seems to be what happens -- the answer to my question is no, one cannot do it. And I'll mark it. – ahmd0 Mar 18 '12 at 19:41