C#:XmlTextWriter.WriteElementString 在空字符串上失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1176202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:04:41  来源:igfitidea点击:

C#: XmlTextWriter.WriteElementString fails on empty strings?

c#xmlxmltextwriterwriteelementstring

提问by Roee Adler

I'm using XmlTextWriterand its WriteElementStringmethod, for example:

我正在使用XmlTextWriter它的WriteElementString方法,例如:

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:

预期的 XML 输出是:

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

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

但是,如果例如 inputEmail 为空,我得到的结果 XML 如下:

<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?

我究竟做错了什么?有没有办法以简单的方式实现我的预期结果XmlTextWriter

采纳答案by Philippe Leybaert

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

你的输出是正确的。没有内容的元素应该写成<tag/>.

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

您可以通过调用 WriteFullEndElement() 强制使用完整标记

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

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

<Email></Email>当 inputEmail 为空时将输出。

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();

回答by RaYell

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

它不会失败<Tag/>只是一个捷径<Tag></Tag>

回答by John Saunders

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).

这避免了异常情况下的资源泄漏,并使用正确的方式创建 XmlReader(自 .NET 2.0 起)。

回答by PhoenixDev

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 = trueand add it to the XmlWriter.

诀窍是设置XmlWriterSettings.Indent = true并将其添加到XmlWriter

Edit:

编辑:

Alternatively you can also use

或者,您也可以使用

w.Formatting = Formatting.Indented;

instead of adding an XmlWriterSettings.

而不是添加一个XmlWriterSettings.