Assessing Xml node values with xmlnamespace

I am trying to access "shl:Value" node using c# but getting null or run time error. I like avoid using big loops.

Code:

XmlDocument objXmlDoc1 = new XmlDocument();
objXmlDoc1.Load("C:\\inetpub\\Jobtrain\\test.xml");

XmlReader reader = XmlReader.Create(new StringReader(objXmlDoc1.InnerXml));
XElement root = XElement.Load("C:\\inetpub\\Jobtrain\\test.xml");
XmlNameTable nameTable = reader.NameTable;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("shl", "http://www.shl.com");

sScore_Raw = root.XPathSelectElement(".//shl:Scales/shl:Scale[@Tag='SHL_VAT_GENERAL_ABILITY_SCREEN_SCORE']/shl:Values/shl:Value[@Tag='RAW SCORE']", namespaceManager).Value;

Error On following lines

sJTCandidateID = root.XPathSelectElement(".//ClientOrderId/IdValue[@name='ThirdPartyCandidateId']", namespaceManager).Value;
sTestStatus = root.XPathSelectElement(".//AssessmentStatus/Status", namespaceManager).Value;

XML:

<AssessmentResult xmlns:shl="http://www.shl.com" xmlns="http://ns.hr-xml.org/2004-08-02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.hr-xml.org/2004-08-02  AssessmentResult.xsd http://www.shl.com AssessmentResultUserArea.xsd">
<ClientOrderId>
<IdValue name="ThirdPartyCandidateId">XX-00076-000-00007</IdValue>
</ClientOrderId>
<AssessmentStatus><Status>Completed</Status><Details>Successful</Details><StatusDate>2014-04-17</StatusDate></AssessmentStatus>
<UserArea>
<shl:CandidateResult>
<shl:Report />
<shl:Score IsRescored="0">
<shl:Scales>
<shl:Scale Tag="SHL_VAT_GENERAL_ABILITY_SCREEN_SCORE" IsGeneric="1">
    <shl:Name Default="General Ability Screen Score"><shl:Translation ID="en-gb">General Ability Screen Score</shl:Translation></shl:Name>
    <shl:Values>
        <shl:Value Tag="RAW SCORE">0</shl:Value><shl:Value Tag="OTHER">0</shl:Value>
    </shl:Values>
</shl:Scale>
</shl:Scales>
</shl:Score>
<shl:StatusCode>0</shl:StatusCode>
</shl:CandidateResult>
</UserArea></AssessmentResult>
Jon Skeet
people
quotationmark

If your only requirement is to get at the shl:Value element, then that's really easy with LINQ to XML - I wouldn't personally use XPath to do it:

XNamespace shl = "http://www.shl.com";
XDocument doc = XDocument.Load(...);
XElement element = doc.Descendants(shl + "Scale")
                      .Where(x => (string) x.Attribute("Tag") == "...")
                      .Descendants(shl + "Value")
                      .Single();

And for the Status and ClientOrderId parts:

// Default namespace in the document
XNamespace ns = "http://ns.hr-xml.org/2004-08-02";
XDocument doc = XDocument.Load(...);
XElement orderId = doc.Descendants(ns + "ClientOrderId")
                      .Single(x => (string) x.Attribute("name") == "ThirdPartyCandidateId");
XElement status = doc.Descendants(ns + "AssessmentStatus")
                     .Elements(ns + "Status")
                     .Single();

people

See more on this question at Stackoverflow