0

I'm having trouble serializing a list to XML, if the list is empty or null, I still want the element , unfortunately, I can't get this to work, the element is omitted.

public partial class Car {
    string model;
    string brand;
} 
[XmlArray(IsNullable = true)]
[XMLArrayItem(Car)]
public list<Car> Cars 
{
    get { return _cars; 
    set { _cars = value;
}

I serialize in the following way

private string SerializeObject(object obj)
{
    string res;

       XmlWriterSettings writerSettings = new XmlWriterSettings();
       writerSettings.DoNotEscapeUriAttributes = true;
       writerSettings.Encoding = Encoding.UTF8;
       writerSettings.CheckCharacters = true;
       writerSettings.Indent = false;
       writerSettings.NewLineHandling = NewLineHandling.None;
       writerSettings.WriteEndDocumentOnClose = true;
       writerSettings.OmitXmlDeclaration = true;

       XmlWriterSettings xmlWriterSettings = writerSettings;
       XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
       StringWriter strWriter = new StringWriter();
       XmlWriter xmlWriter = XmlWriter.Create(strWriter, xmlWriterSettings);
       xmlSerializer.Serialize(xmlWriter, obj);

       var doc = XElement.Parse(strWriter.ToString());
       doc.Descendants().ToList().Where(d => d.Value.Length == 0 || d.Value == "0").Remove();
       res = doc.ToString();
       return res;
}

Any suggestions?

Recusiwe
  • 1,594
  • 4
  • 31
  • 54
  • Can you share a [mcve]? Your code won't compile as-is, and your `Car` type has no public properties. – dbc Jul 28 '23 at 19:37
  • The problem is that your `SerializeObject(object obj)` is stripping the `` and ``. If I just invoke the serializer with default options the tags are serialized, see https://dotnetfiddle.net/mnBLMo. What is the purpose of your `SerializeObject(object obj)` method? Why are you using it? Specifically `doc.Descendants().ToList().Where(d => d.Value.Length == 0 || d.Value == "0").Remove();` seems to be what is removing the tags. If you don't want that, don't do it. – dbc Jul 28 '23 at 19:44
  • I fiddled around with it to try to control which elements I want to show even when empty, since I want to be selective about it – Recusiwe Jul 28 '23 at 19:52
  • You can prevent a property from being serialized by applying [`[XmlIgnore]`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlignoreattribute?view=net-7.0). Other than that, what answer do you want for your question? The only answer that can currently be given is, *The element is not omitted, you are removing it.* Beyond that there's not much we can help with unless you can specify what you are trying to accomplish with `SerializeObject(object obj)`. – dbc Jul 28 '23 at 20:25
  • Will [XmlIgnore] only omit empty or null elements? – Recusiwe Jul 28 '23 at 22:52
  • No, but you could use a `ShouldSerialize{PropertyName}()` method to suppress serialization of specific properties when unwanted. See e.g. [ShouldSerialize*() vs *Specified Conditional Serialization Pattern](https://stackoverflow.com/a/37842985). – dbc Jul 28 '23 at 22:57

0 Answers0