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

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;
 
J

Jon Skeet [C# MVP]

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

roto23

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;
]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top