I can't get the value of an attribute in an xml file

  • Thread starter Thread starter roto23
  • Start date Start date
R

roto23

I am able to correctly XPath to the correct Node in the XML file,
however when I try to get the value of an attribute on that node, it
doenot work.

intX gets a value of 1 so I am sure the XPath query is correct but this
line retunrs an empty string...
strDBFactor = NodeIter.Current.GetAttribute("DeathBenefitFactor", "");

here is the complete code..

string strDBFactor;
int intX;
strXmlQuery = "Root/DataPoint[@Age='40' and @RateClass='P' and
@Sex='F']";
docNav = new XPathDocument(strXmlFileNameAndPath);
nav = docNav.CreateNavigator();
NodeIter = nav.Select(strXmlQuery);
intX = NodeIter.Count;
strDBFactor = NodeIter.Current.GetAttribute("DeathBenefitFactor", "");
return strDBFactor;
 
I am able to correctly XPath to the correct Node in the XML file,
however when I try to get the value of an attribute on that node, it
doenot work.

intX gets a value of 1 so I am sure the XPath query is correct but this
line retunrs an empty string...
strDBFactor = NodeIter.Current.GetAttribute("DeathBenefitFactor", "");

here is the complete code..

string strDBFactor;
int intX;
strXmlQuery = "Root/DataPoint[@Age='40' and @RateClass='P' and
@Sex='F']";
docNav = new XPathDocument(strXmlFileNameAndPath);
nav = docNav.CreateNavigator();
NodeIter = nav.Select(strXmlQuery);
intX = NodeIter.Count;
strDBFactor = NodeIter.Current.GetAttribute("DeathBenefitFactor", "");
return strDBFactor;

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Thanks for trying, but I got it working using this method..


private System.Xml.XmlDocument objXmlDoc;
private System.Xml.XmlNode objXmlNode;

public string SolveFordeathBenefit()
{
string strDBFactor;

objXmlDoc = new XmlDocument();
objXmlDoc.Load(strXmlFileNameAndPath);
strXmlQuery = "Root/DataPoint[@Age='40' and @RateClass='P' and
@Sex='F']";
objXmlNode = objXmlDoc.SelectSingleNode(strXmlQuery);
strDBFactor =
objXmlNode.Attributes.GetNamedItem("DeathBenefitFactor").Value;
return strDBFactor;
]
 
Back
Top