Search code examples
c#xmlmaintainability

Is there any easy way to develop easily maintainable code where XML parameters must have the same name as class variables in C#?


Let's say that my code has a class defined as so:

class test{
    bool var;
    public test(){}
}

In my main function, I create an object of the sort, and set var to true.

After that I parse some XML files that contain the following:

<param name="var" value="false"/>

I read that var must be false, and set it to false.

This is great and all, but the issue is maintainability. It seems like a lot of trouble to maintain this, since if someone changes a variable name in the code, they will have to change all of the XML files.

Is there some easy way around this? Or at least an easy way to maintain this? The only thing I can think of right now is some meta-script that will somehow edit the XML and c# code, but this seems like a lot of work, and honestly quite hacky.


Solution

  • The code sample below might be of some help for you.

    Instead of declaring a variable "name" and "value" in XML, use XML serialization provided by .NET to parse the XML into appropriate variables. Feel free to remove the "name" from your XML altogether, or turn it into a device for clearly expressing the purpose of the value. I have updated your XML to use elements (more readable) instead of attributes below:

    XML Sample

    <Test>
        <Params>
            <Param>
                <Name>Meaningful Parameter Name</Name>
                <Value>1</Value>
            </Param>
            <Param>
                <Name>Meaningful Parameter Name2</Name>
                <Value>0</Value>
            </Param>
        </Params>
    </Test>
    

    It is in the code that we assign XML elements to variables, using xml serialization. As you can see, the Param class will contain a string name and a boolean value, which will be read from the XML elements "Name" and "Value". This allows you to change your variable names without having to worry about updating the XML to reflect them. If you changed your mind and wanted to rename "Value" in the XML to something different, you would just need to update the code in one place [XmlElement("Value")] with the new name defined in the XML. Simple! :)

    Good luck!

    Code Example

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Linq;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication1
    {
        public class Program
        {
            private static void Main(string[] args)
            {
                string testXML = @"<Test>
                                        <Params>
                                            <Param>
                                                <Name>Meaningful Parameter Name</Name>
                                                <Value>1</Value>
                                            </Param>
                                            <Param>
                                                <Name>Meaningful Parameter Name2</Name>
                                                <Value>0</Value>
                                            </Param>
                                        </Params>
                                   </Test>";
    
                XDocument doc = XDocument.Parse(testXML);
                XmlSerializer serializer = new XmlSerializer(typeof (Test));
                Test testDeserialized = (Test) serializer.Deserialize(doc.CreateReader());
    
                foreach (Param param in testDeserialized.Params)
                {
                    Console.WriteLine("Name: " + param.Name + ", Value: " + param.Value);
                }
    
                Console.ReadLine();
            }
        }
    
        [XmlRoot]
        public class Test
        {
            [XmlArray("Params")]
            [XmlArrayItem("Param", typeof (Param))]
            public Param[] Params { get; set; }
        }
    
        public class Param
        {
            [XmlElement("Name")]
            public string Name { get; set; }
    
            [XmlElement("Value")]
            public bool Value { get; set; }
        }
    }