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

StackOverflow https://stackoverflow.com/questions/16953509

  •  31-05-2022
  •  | 
  •  

Question

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.

Was it helpful?

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; }
    }
}

OTHER TIPS

You have a few options:

  • If you don't control the format of the XML files, you can just write some plumbing code which reads from the attribute names of the XML into whatever your property names are. There's no technical reason they must be the same.

  • If you are generating the XML files at runtime, you can use reflection to get the property names and set the XML file attribute names accordingly. They will always be in sync this way.

  • If you are creating the XML files at compile-time and you are a Visual Studio user, I would suggest looking into T4 Templates. They're a great tool to generate files in a consistent, reusable, and strongly typed way.

NetReflector is good at this. CruiseControl.net uses it to create objects from xml configuration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top