处理 XmlTextWriter 和空节点

Dealing with XmlTextWriter and null node

我有一些代码可以即时创建 xml 文件。有时节点有值,有时没有。我需要打开和关闭标签 <hour>12:00</hour><hour></hour> 即使没有价值,但我找不到解决方案。值为空的地方我得到 <hour />

XmlTextWriter writer = new XmlTextWriter(path + "/" + CustomerId + "/Data/" + CustomerId + ".xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("data");

if (paravalue == null || paravalue == "")
{
    writer.WriteStartElement(para);
    writer.WriteString("");
    writer.WriteEndElement();
}
else
{
    writer.WriteStartElement(para);
    writer.WriteString(paravalue);
    writer.WriteEndElement();
}

使用 WriteFullEndElement 代替 WriteEndElement