0

I have followed a good tutorial that shows how to make an Automation Framework in C# Selenium. The config file is in XML at the moment, but I wanted some more practice and change it to a .json file.

At the moment we are using the namespace System.Xml.XPath; and my question is, are there a similar for JSON? Lets say "System.Json;" that works the same as my XML reader. So I don't need to refactor to much code, or is it unavoidably?

This is how it works at the moment.

//Initialize
ConfigReader.SetFrameworkSettings();

public class ConfigReader
{

        public static void SetFrameworkSettings()
        {
            XPathItem aut;

            string strFilename = Environment.CurrentDirectory.ToString() + "\\Config\\GlobalConfig.xml";
            FileStream stream = new FileStream(strFilename, FileMode.Open);
            XPathDocument document = new XPathDocument(stream);
            XPathNavigator navigator = document.CreateNavigator();

            //Get XML Details and pass it in XPathItem type variables
            aut = navigator.SelectSingleNode("AutoFramework/RunSettings/AUT");
            Settings.AUT = aut.Value.ToString();
        }
    }

public class Settings
{
    public static string AUT { get; set; }
}

Would be awesome if you could just change this two lines

XPathDocument document = new XPathDocument(stream);
XPathNavigator navigator = document.CreateNavigator()

And the XpathItem

Cheers

thomasvdb
  • 739
  • 1
  • 11
  • 32
Dymond
  • 2,158
  • 7
  • 45
  • 80
  • 1
    I think the following answer about JSON.NET can point you to the correct solution: http://stackoverflow.com/a/17788118/3531995 You can pass a stream to the JSON.NET library but you will also need to update the line ```aut = navigator.SelectSingleNode("AutoFramework/RunSettings/AUT");``` because this is XML specific. – thomasvdb Jan 11 '17 at 13:18
  • @thomasvdb Will read! tnx – Dymond Jan 11 '17 at 13:22

1 Answers1

1

I would recommend using Newtonsoft.Json which is available from Nuget. To "reuse" your current code you would have to make some basic SettingsConverter first:

public static class SettingsConverter
{
    public static string FromXmlToJson(string xml)
    {
        Settings settings = null;
        // read your xml file and assign values to the settings object
        // now you can "serialize" settings object into Json
        return JsonSerialization.Serialize(settings);
    }

    public static string FromJsonToXml(string json)
    {
        Settings settings = JsonSerialization.Deserialize<MeSettings>(json);
        // save settings using your "xml" serialization
        return xmlString; // return xml string
    }
}

To use these methods I'm using this JsonSerialization helper class :

public static class JsonSerialiation
{
    public static string Serialize<T>(T obj)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(stream))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serializer(writer, obj);
                writer.Flush();
                stream.Position = 0;
                using (StreamReader reader = new StreamREader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }

    public static T Deserialize<T>(string jsonString)
    {
        using (JsonTextReader reader = new JsonTextReader(new StringReader(str)))
        {
            JsonSerializer serializer = new JsonSerializer();
            return serializer.Deserialize<T>(reader)
        }
    }
}

Above example requires from you to change your Settings class from static :

public class Settings
{
    public static string AUT { get; set; }
}

To instance :

public class Settings
{
    public string AUT { get; set; }
}

If you prefer to keep it as static. You should use JObject from Newtonsoft.Json library :

JObject obj = JObject.Parse(jsonString);
Settings.AUT = obj.SelectToken("AUT").Value<string>();

You can always use JsonConvert.Serialize<T>() and JsonConvert.Deserialize<T>() methods instead of the JsonSerialization helper class that I've made but in my opinion the less control you have over your code the bigger the problems will be.

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30