2

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 >

3
  • 2
    possible duplicate of C#: XmlTextWriter.WriteElementString fails on empty strings? Commented Mar 17, 2012 at 23:12
  • Yes, it is a duplicate and that link answers my question. Thank you. Commented Mar 17, 2012 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... Commented Mar 17, 2012 at 23:47

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Is it supposed to be XmlTextWriter, or just XmlWriter? Because I can't seem to make it work with jusr XmlWriter...
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.
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"/>

Comments

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", " ");

3 Comments

Yes, I know that. For my purpose two make a difference. < c1/ > is treated as null and < c1 ></ c1 > - as an empty string.
@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 (w3.org/TR/xmlschema-1/#xsi_nil) which is designed for this.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.