C# XmlSerializer ensure that null elements are writen in the xml file as <XmlElement/>

ptuga 21 Reputation points
2023-01-07T22:49:40.537+00:00

Hello,

I've been using this method to write xml files from classes with success so far:

		public static void WriteDataToXMLFromClass<T>(this T Class, T classIn, string fileToWriteDataTo)  
		{  
			XmlSerializer serializer = new XmlSerializer(typeof(T), string.Empty);  
			XmlWriterSettings wSettings = new XmlWriterSettings  
			{  
				Indent = true,  
				CloseOutput = true,  
			};  
			XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();  
			namespaces.Add("", "");  
  
			using (XmlWriter writer = XmlWriter.Create(fileToWriteDataTo, wSettings))  
			{  
				serializer.Serialize(writer, classIn, namespaces);  
			}  
		}  
  

For a very specific case I have the following class:

    [XmlRoot(ElementName = "Setting")]  
    	public class Setting  
    	{  
    		[XmlElement(ElementName = "Val")]  
    		public List<string> Val { get; set; }  
    		[XmlAttribute(AttributeName = "name")]  
    		public string Name { get; set; }  
    		[XmlAttribute(AttributeName = "type")]  
    		public string Type { get; set; }  
     
    	}  

The way I need to store the data is in a way that, all my ID, Index, IsDataId and Name have the same number of Val items. In the example 2 items on each.
My List<string> Val needs to have null values sometimes, but my xml should always consider the same amount of Val elements.

When I run the WriteDataToXMLFromClass method above I get the following output on my xml file:

                  <Setting name="ID" type="int">  
                    <Val>Id1</Val>  
                    <Val>Id2</Val>  
                  </Setting>  
                  <Setting name="Index" type="int">  
                    <Val>Index1</Val>  
                    <Val>Index2</Val>  
                  </Setting>  
                  <Setting name="IsDataId" type="bool">  
                    <Val>IsDataId1</Val>  
                    <Val>IsDataId2</Val>  
                  </Setting>  
                  <Setting name="Name" type="string">  
                    <Val>Name2</Val>  
                  </Setting>  
  

There is only one Val element when I should have two.

What I would like to have should be:

                  <Setting name="ID" type="int">  
                    <Val>Id1</Val>  
                    <Val>Id2</Val>  
                  </Setting>  
                  <Setting name="Index" type="int">  
                    <Val>Index1</Val>  
                    <Val>Index2</Val>  
                  </Setting>  
                  <Setting name="IsDataId" type="bool">  
                    <Val>IsDataId1</Val>  
                    <Val>IsDataId2</Val>  
                  </Setting>  
                  <Setting name="Name" type="string">  
                    <Val/>     <------------------------------------------ this <Val/> should be written even if the string element in the List is null                      
                    <Val>Name2</Val>  
                  </Setting>  

This is because the 1st value of the "Name" attribute 'name' is null in the list Name.Val[0] = null.
Setting the list item to 'string.Empty' will not work.
The only solution is to write <Val/> for every list element that is null.

I assume there is a work around.
Maybe someone can point me in the right direction.

Many thanks in advance.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
{count} votes