I'm creating a new XML Document that has some tags that have no innertext.
If I populate the innertext with some data then it works as needed in this instance, namely you get opening tag, innertext and closing tag all on one line:
<testtag>somevalue</testtag>
The problem arises with tags with no values. These need to be displayed in the same manner as above:
<testtag></testtag>
However, if I populate the innertext with an empty string it adds a carriage return & line feed which is not what the importing software needs.
<testtag>
</testtag>
If I leave innertext null then I get a self closing tag . Again this isn't as required.
I've tried removing /n and /r from innertext as some other posts have suggested but as soon as the document is saved the same issue occurs.
The following code does not suffer from the issue you describe. If this code sample does not put you on the right track please update your question with the code you are using so that we can better diagnose.
class Program
{
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings {Indent = true};
XmlWriter writer = XmlWriter.Create("C:\\Users\\elaforc\\temp.xml", settings);
XmlDocument xmlDocument = new XmlDocument();
XmlElement parentElement = xmlDocument.CreateElement("Parent");
XmlElement childElement = xmlDocument.CreateElement("Child");
childElement.InnerText = "";
parentElement.AppendChild(childElement);
xmlDocument.AppendChild(parentElement);
xmlDocument.Save(writer);
}
}
Generates
<?xml version="1.0" encoding="utf-8"?>
<Parent>
<Child></Child>
</Parent>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With