I have an XML file like below:
<sections>
<section name="516604">
<item key="Terminal" value="254726387322" />
<item key="UserName" value="SomeName" />
<item key="Pass" value="XXXX" />
</section>
<section name="802200">
<item key="Terminal" value="254726402010" />
<item key="UserName" value="SomeOtherName" />
<item key="Pass" value="XXXX" />
</section>
</sections>
How can I have a C# function that returns a string containing a field value like below:
public string GetXMLValue(string Section, string Terminal)
{
return string TerminalOfSection
}
The above method should return the XML Tag value "Terminal" of a given Section say "516604" as a string.
Thank You.
I did some modificatins to the XML to suite my purpose:
<xml version="1.0" encoding="UTF-8"?>
<516604>
<Terminal>254726387322</Terminal>
<UserName>SomeUser</UserName>
<Pass>XXXX</Pass>
</516604>
<802200>
<Terminal>254726402010</Terminal>
<UserName>SomeOtherUser</UserName>
<Pass>XXXX</Pass>
</802200>
And here is the code to get the exact tag from the XML and save it in a string like so:
public static string GetXMLValue(string XMLSection, string XMLInput, string XMLtagValue)
{
string retval = string.Empty, FirstXMLSection = string.Empty, LastXMLSection = string.Empty,
FirstXMLtagValue = string.Empty, LastXMLtagValue = string.Empty;
//Tag the XML section to retrieve value from
FirstXMLSection = "<" + XMLSection + ">";
LastXMLSection = "</" + XMLSection + ">";
//Tag the XML element to retrieve value from
FirstXMLtagValue = "<" + XMLtagValue + ">";
LastXMLtagValue = "</" + XMLtagValue + ">";
if (XMLInput.IndexOf(XMLtagValue) != -1)
{
//Retrieve XML Section & save in a string retval
retval = XMLInput.Substring(XMLInput.IndexOf(FirstXMLSection));
retval = retval.Substring(FirstXMLSection.Length);
retval = retval.Substring(0, retval.IndexOf(LastXMLSection));
//Retrieve XML element & still save in a string retval
retval = retval.Substring(retval.IndexOf(FirstXMLtagValue));
retval = retval.Substring(FirstXMLtagValue.Length);
retval = retval.Substring(0, retval.IndexOf(LastXMLtagValue));
}
return retval;
}
So if I feed section as 516604,the XML Text as XMLInput & terminal as the Tag to get the Value the output will be: 254726387322. Any improvements/demerits are welcomed. Thanks.