-->

Can we force XmlWriter to issue rather t

2019-01-15 12:35发布

问题:

By default,

someXmlWriter.WriteElementString("my-tag", someString);

produces <my-tag />

I looked around XmlWriterSettings class for possible options that would force the writer to produce <my-tag></my-tag> instead but didn't find anything.

Is there a simple way of forcing the XmlWriter to issuing empty elements with "open tag, close tag" rather than with the short-hand form?

Edit:
Yes! I realize that with regards to XML validity the two notations are equivalent, valid and all... I'm however working with legacy code which parses such XML using Read(), i.e. at node level (!) and fumbles things up by Read()-ing when on an empty node...

Hence my question comes in the context of limiting the amount of changes to this legacy code. The question is indeed overlapping with this SO question as suggested; none of the options offered there are however easily applicable to my situation.

回答1:

There is no option setting to do that in .Net. They are the same thing and any parser should be able to handle valid XML.



回答2:

This worked for me:

writer.WriteStartElement("my-tag");
writer.WriteRaw(someString);
writer.WriteFullEndElement();

WriteEndElement still self closed the tag



回答3:

If you get the message Extension method can only be declared in non-generic, non-nested static class as the word this may be highlighted, simply create an extension class as shown below:

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

Hope this proves useful :-)



回答4:

Try this:

x.WriteStartElement("my-tag"); 

//Value of your tag is null

If (<"my-tag"> == "")

{
  x.WriteWhitespace("");
}else
  x.WriteString(my-tag);

x.WriteEndElement();


回答5:

Have you tried something like this:

if (someString.Length > 0)
{
  someXmlWriter.WriteElementString("my-tag", someString);
}
else
{
  someXmlWriter.WriteStartElement("my-tag");
  someXmlWriter.WriteEndElement("my-tag");
}

Maybe make a utility class with that as a member function.



回答6:

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.



回答7:

I use the next code:

if (string.IsNullOrEmpty(myStringValue))
{
   myWriter.WriteStartElement("mytag");
   myWriter.WriteFullEndElement();
}
else
{
   myWriter.WriteElementString("mytag", myStringValue);
}


回答8:

I just ran into this exact same issue, and while it seems like kind of a hack, this did indeed work and was very unintrusive to the existing code I had to work with.

private static XElement MakeElementLongHand(this XElement rootElem)
{
   rootElem.Value = " ";
   rootElem.Value = string.Empty;
   return rootElem;
}