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:
- 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.
- 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.
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.