Reputation: 11471
I am having an issue getting the value of an XML, it is loading it in the XML document when i debug, but it is not finding the value or the node i want to access. Here is the xml. The value i want to get is "Active". Now this XML is not a file or anything , is being passed as a string... (can not modify this part) have only access to the class where i can create functions to access it but can not modify the actual code getting the values and passing it as a "xml string"
<Clients>
<BillingCycle>30</BillingCycle>
<Category>1</Category>
<Type>Admin</Type>
<AddressCat>3</AddressCat>
<ZipCodeCat>5</ZipCodeCat>
<ClientManager>
<UserID>5</UserID>
<ZPVal>1</ZPVal>
<DRY1>Test</DRY1>
<Active>1</Active>
</ClientManager>
</Clients>
C# code here
public bool IsActive(int ClientID, int VassID)
{
bool isActive = false;
HelperClass helper = new HelperClass();
XmlDocument xml = new XmlDocument();
//at this point i can see the data was stored in the xml when debugging
xml.LoadXml(helper.GetClientXML(ClientID, VassID));
//have tried the following do not woerk
// XmlNode node = xml.SelectSingleNode ("/Clients/ClientManager/Active/text()");
XmlNode node = xml.SelectSingleNode("Clients/ClientManager/Active");
int isActiveVal = Convert.ToInt32(node.Value);
if (isActiveVal == 1)
{
isActive = true;
}
return isActive;
}
Upvotes: 1
Views: 1789
Reputation: 50215
This line could work:
//XmlNode node = xml.SelectSingleNode ("/Clients/ClientManager/Active/text()");
var activeTextNodes = xml.SelectNodes("/Clients/ClientManager/Active/text()");
... but it will select a set of Active text() values, which you probably don't want.
You could access the InnerText
value of the node you found:
XmlNode node = xml.SelectSingleNode("Clients/ClientManager/Active");
int isActiveVal = Convert.ToInt32(node.InnerText);
Or you could access the value of the First Child:
XmlNode node = xml.SelectSingleNode("Clients/ClientManager/Active");
int isActiveVal = Convert.ToInt32(node.FirstChild.Value);
All this being said, you should use int.TryParse
instead of Convert.ToInt32
because it won't throw an exception on non-integer data.
Upvotes: 1