Reputation: 817
I can find lots of people and examples that ask how to hide the XML tag but what I don't find is when the value is null or empty string how to get it to write the tag. For me no matter what I've tried including adding that XmlElement that marks it as IsNullable false/true (tried both) the tag for MyProperty is never written unless it has a value.
I've tried
[XmlElement(IsNullable = true)]
and
[XmlElement(IsNullable = false)]
neither makes a difference
This is just a mockup of the real code the save is done this way as it's saving it as part of an existing XmlDocument so there is other code that loads and finds where it needs to add it and saves the entire document again. Including all of the code and then removing IP would be a lot of work and pages of code.
public class MyClass
{
public string MyProperty { get; set; }
}
var myClass = new MyClass();
var xmlSerializer = new XmlSerializer(MyClass.GetType());
var xmlSerializerNamespaces = new XmlSerializerNamespaces();
xmlSerializerNamespaces.Add("", "");
var xmlWriterSettings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
Async = true
};
using var stringWriter = new StringWriter();
using (var xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
{
xmlSerializer.Serialize(xmlWriter, myClass, xmlSerializerNamespaces);
await xmlWriter.FlushAsync();
xmlWriter.Close();
xmlWriter.Dispose();
}
await stringWriter.FlushAsync();
stringWriter.Close();
await stringWriter.DisposeAsync();
var xDocument = XDocument.Parse(stringWriter.ToString());
xDocument.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();
xDocument.Save(myClass);
Upvotes: 0
Views: 1652
Reputation: 5009
Try the following it's been tested using .NET 6:
using statements:
using System.Xml;
using System.Xml.Serialization;
SerializeToXMLFile:
private void SerializeToXMLFile(object obj, string xmlFilename)
{
if (String.IsNullOrEmpty(xmlFilename))
throw new Exception($"Error: XML filename not specified. (xmlFilename: '{xmlFilename}')");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(xmlFilename, settings))
{
//specify namespaces
System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add(string.Empty, "urn:none");
//create new instance
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
//write XML to file
serializer.Serialize(writer, obj, ns);
}
}
Option 1:
Create class (name: MyClass)
[XmlRoot("myclass")]
public class MyClass
{
[XmlElement(ElementName = "myproperty")]
public string? MyProperty { get; set; } = string.Empty;
}
Note: Set the value to string.Empty
so that it's saved when the value isn't set.
Result:
Option 2:
Create class (name: MyClass)
[XmlRoot("myclass")]
public class MyClass
{
[XmlElement(ElementName = "myproperty", IsNullable = true)]
public string? MyProperty { get; set; }
}
Result:
Usage:
MyClass myClass = new MyClass();
SerializeToXMLFile(myClass, @"C:\Temp\Test.xml");
Resources:
Upvotes: 2