xml - C#: XmlTextWriter.WriteElementString fails on empty strings? -


I am using XmlTextWriter and its WriteElementString method, For example:

  XmlTextWriter author = new XmlTextWriter ("filename.xml", tap); Author.WriteStartElement ("user"); Author. Write element string ("username", InputUser name); Author. Write element string ("email", input email); Writer.WriteEndElement (); Writer.Close ();  

Expected XML output is:

  & lt; Users & gt; & Lt; Username & gt; Price & lt; / User Name & gt; & Lt; Email & gt; Price & lt; / Email & gt; & Lt; / User & gt;  

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

   

While I expect this to happen:

   

What am I doing wrong? XmlTextWriter ?

What is the method to get my expected results in a simple way

Your output is correct with an element .

You can force Write to WriteFullEndElement () and use the full tag

  writer.WriteStartElement ("email"); Author.WriteString (inputEmail); Writer.WriteFullEndElement ();  

This input & lt; Email & gt; & Lt; / Email & gt; when the input email is empty.

Once you want to do this, you can create an extension method:

  public static zero WriteFullElementString (this XmlTextWriter author, string localname, string value) { Author.WriteStartElement (localName); (Value) author.WriteString; Writer.WriteFullEndElement (); }  

Then your code will be created:

  the author. WriteStartElement ("user"); Author. WrititeFullElementString ("user name", inputUserName); Author. WrititeFullElementString ("email", input email); Writer.WriteEndElement ();  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -