C#: XmlTextWriter.WriteElementString fails on empty strings?
Asked Answered
S

5

9

I'm using XmlTextWriter and its WriteElementString method, for example:

XmlTextWriter writer = new XmlTextWriter("filename.xml", null);

writer.WriteStartElement("User");
writer.WriteElementString("Username", inputUserName);
writer.WriteElementString("Email", inputEmail);
writer.WriteEndElement();

writer.Close();

The expected XML output is:

<User>
    <Username>value</Username>
    <Email>value</Email>
</User>

However, if for example inputEmail is empty, the result XML I get as as follows:

<User>
    <Username>value</Username>
    <Email/>
</User>

Whereas I would expect it to be:

<User>
    <Username>value</Username>
    <Email></Email>
</User>

What am I doing wrong? Is there a way to achieve my expected result in a simple way using XmlTextWriter?

Slivovitz answered 24/7, 2009 at 7:18 Comment(0)
S
23

Your output is correct. An element with no content should be written as <tag/>.

You can force the use of the full tag by calling WriteFullEndElement()

writer.WriteStartElement("Email");
writer.WriteString(inputEmail);
writer.WriteFullEndElement();

That will output <Email></Email> when inputEmail is empty.

If you want to do that more than once, you could create an extension method:

public static void WriteFullElementString(this XmlTextWriter writer,
                                          string localName, 
                                          string value)
{
    writer.WriteStartElement(localName);
    writer.WriteString(value);
    writer.WriteFullEndElement();
}

Then your code would become:

writer.WriteStartElement("User");
writer.WriteFullElementString("Username", inputUserName);
writer.WriteFullElementString("Email", inputEmail);
writer.WriteEndElement();
Stupor answered 24/7, 2009 at 7:22 Comment(2)
I used the exact same solution you provided, and the WriteFullEndElement() method does not seem to work... Methinks this method is faulty.Lipetsk
I found a workaround in C#. If the (string.Length==0), then substitute the empty string with a newline escape character, ie "\n"Lipetsk
C
5

It doesn't fail <Tag/> is just a shortcut for <Tag></Tag>

Catfall answered 24/7, 2009 at 7:21 Comment(2)
It is possible to force it to output <Tag></Tag>?Slivovitz
If that is an issue, you have all right to yell at whoever requires you to write the tags out in full; they need to follow basic XML specs.Unite
S
2

Your code should be:

using (XmlWriter writer = XmlWriter.Create("filename.xml"))
{
    writer.WriteStartElement("User");
    writer.WriteElementString("Username", inputUserName);
    writer.WriteElementString("Email", inputEmail);
    writer.WriteEndElement();
}

This avoids resource leaks in case of exceptions, and uses the proper way to create an XmlReader (since .NET 2.0).

Semilunar answered 24/7, 2009 at 13:22 Comment(0)
A
0

Leaving this here in case someone needs it; since none of the answers above solved it for me, or seemed like overkill.

FileStream fs = new FileStream("file.xml", FileMode.Create);

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter w = XmlWriter.Create(fs, settings);
w.WriteStartDocument();
w.WriteStartElement("tag1");
w.WriteStartElement("tag2");
w.WriteAttributeString("attr1", "val1");
w.WriteAttributeString("attr2", "val2");
w.WriteFullEndElement();
w.WriteEndElement();
w.WriteEndDocument();

w.Flush();
fs.Close();

The trick was to set the XmlWriterSettings.Indent = true and add it to the XmlWriter.

Edit:

Alternatively you can also use

w.Formatting = Formatting.Indented;

instead of adding an XmlWriterSettings.

Alemannic answered 9/9, 2015 at 7:56 Comment(0)
E
0

Tried solving this with another approach, might need optimization.

public class SerializeConfig<T> where T : class
{
        public static string Serialize(T type)
        {
            var settings = new XmlWriterSettings
            {
                Encoding = Encoding.UTF8,
                Indent = true,
                OmitXmlDeclaration = true
            };
            var sb = new StringBuilder();
            var serializer = new XmlSerializer(type.GetType());
            using (var writer = XmlWriter.Create(sb, settings))
            {
                serializer.Serialize(writer, type);
            }

            return sb.ToString().FixXmlClosingTags();
        }
}
internal static class InsertStringExtention
{
    public static string FixXmlClosingTags(this string xmlString)
    {
        var sb = new StringBuilder();
        var xmlTags = xmlString.Split('\r');
        foreach (var tag in xmlTags)
        {
            if (tag.Contains("/>"))
            {
                var tagValue = tag.Replace("<", "").Replace("/>", "").Trim();
                var firstPart = tag.Substring(0, tag.IndexOf('<'));
                var newTag = $"{firstPart}<{tagValue}></{tagValue}>";
                sb.Append(newTag);
            }
            else
            {
                sb.Append(tag);
            }
            
        }
        return sb.ToString();
    }
}
Evelyn answered 5/11, 2022 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.