0

As of now, my code is working perfectly and there are no issues but I wanted input on if there's an easier way to reading XML files.

So here's a summary on what I'm doing:

  1. I have a large program that deals with several outside factors. At one point, I am given an XML file that has many different elements and child nodes.
  2. For each anticipated element, I created a class that has variables which will hold all the data from the node.

This isn't what my XML file looks like but here is a simple example mocking it so you have a better idea what I'm working with.

   -<status>
   -<CallBase FilePath="EU\Domain Users" CallValid="True" CodedPath="D:\Users\CASE\" 
   TracePath="D:\Users\CASE\" StartEnviorment="" ServerLogs="" ServerPath="" ConfigPort="217757" 
   PacePort="217757" Version="2.5.0.73" Version2="10.3.0.73" VersionName="CASEservice" 
   LastTimeCheck="08/05/2020 15:05:07">
   <ValidPermissions Default=""/>
   </CallBase>
   </status>

I created a class for the CallBase element called "CallBase" that contains strings for FilePath, CallValid,CodedPath, TracePath, StartEnviorment, ServerLogs, ServerPath, ConfigPort, PacePort, Version and so on.

Here's how I'm reading this specific node and storing it in a CallBase object:

private CallBase getCallBaseData(List<XmlDocument> xmlDoc, int i)                                             
    {
        var result = xmlDoc[i].DocumentElement.SelectNodes("//CallBase");
        foreach (var node in result)
        {
            var temp1 = node.ToString();
        }

        XmlNode xmlNode = xmlDoc[i].SelectSingleNode("//CallBase");

        XmlSerializer serial = new XmlSerializer(typeof(CallBase));

        CallBase tempresult = (CallBase)serial.Deserialize(new XmlNodeReader(xmlNode));

        return tempresult;
    }

However, now I have a ton of the same functions and I replace the "CallBase" with a different element name. I'm wondering if there's a different way to do this so I only have one function.

5
  • if there's an easier way to reading XML files, sure there is, there's always ways to improve code. My suggestion since you're not having any issues, is head over to Code Review Stack Exchange and create a post there. Commented Aug 5, 2020 at 20:25
  • There are lots of method for passing xml in c#. The best method varies depending on a number of factors 1) Have schema or classes 2) Speed 3) Size of file 4) Amount of data needed 5) structure of xml file 6) Format of output needed. I usually use one of 3 methods 1) Xml Linq 2) Xml Serialize 3) XmlReader (or combination of the 3). Commented Aug 5, 2020 at 20:54
  • @Çöđěxěŕ Oh sorry I didn't know that existed till just now! Thank you :) Commented Aug 5, 2020 at 20:56
  • @jdweng Right, I understand. I'm new to querying XML so I think my way was a bit redundant but working on improving my skills Commented Aug 5, 2020 at 20:57
  • Not really. If the classes you create don't start a root of xml you have to move to the element that matches you root class. Commented Aug 5, 2020 at 21:16

1 Answer 1

2

A set of methods that differ only in the type of elements can be replaced with one generic method.

Fortunately, the name of the xml element matches the name of the class type. We'll get it from there with Type.Name property.

private T GetData<T>(List<XmlDocument> xmlDoc, int i)
{
    var xpath = "//" + typeof(T).Name;

    var result = xmlDoc[i].DocumentElement.SelectNodes(xpath);

    foreach (var node in result)
    {
        var temp1 = node.ToString();
    }

    var xmlNode = xmlDoc[i].SelectSingleNode(xpath);

    var serial = new XmlSerializer(typeof(T));

    var tempresult = (T)serial.Deserialize(new XmlNodeReader(xmlNode));

    return tempresult;
}

Using:

GetData<CallBase>(..., ...);
Sign up to request clarification or add additional context in comments.

2 Comments

You are awesome! I tried doing something similar and it wouldn't work, thanks :)
Alexander, can you add an introductory paragraph to this, to help future readers? We tend to find that answers are more useful if they can be explained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.