Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this piece of code that I am trying to implement.The objective is to get value from one of the nodes.
C#
string str1 = obj.GetStatusSettingsFromCamera("http://10.40.160.73/cgi/status.php?item=mfm&format=xml", "", "");
            XElement xml = XElement.Parse(str1);
            XElement findTag = xml.Elements("Firmware_CRC").FirstOrDefault();

            if (findTag != null) \\ showing null 
            {
                string firmwareTitle =Convert.ToString(findTag.Attribute("title"));
                string currentFirmware = findTag.Value;
            }



XML from URL;

HTML
<Status>
<Summary>
<Uptime>09:56:12</Uptime>
<FastWD>00:01:58</FastWD>
<SlowWD>14:03:48</SlowWD>
<Configuration_Download>Success</Configuration_Download>
<Firmware_CRC title="Compatible versions: a29d.fe3e" class="critical hastooltip">a29d.3db8</Firmware_CRC>
<Core_Version>19.6</Core_Version>
<App_Version>23.4</App_Version>
<CPLD_Version>4BF44D51</CPLD_Version>
</Summary>
<Detectors>
<state>Operating</state>
<detector>8 channel - Lane agile radar: Operating</detector>
<detector>1 channel - Wired light phase detector: Operating</detector>
<detector>Optical Light Phase Detector: Absent</detector>
<self_test>pass[00]</self_test>
<radar_error_counter>0</radar_error_counter>
</Detectors>
</Status>


If I try to find Firmware_CRC the findtag is showing null. It's a child node under Summary.
I know I am going wrong somewhere but can't figure it out.
P.S. : I am new to .NET entirely
Posted
Updated 14-Aug-14 8:15am
v2

The Element method[^] only looks for elements which are direct children of the specified node. It would find the <Summary> and <Detectors> elements, but won't work for the children of those elements.

Try using the Descendants method[^] instead:
C#
XElement findTag = xml.Descendants("Firmware_CRC").FirstOrDefault();
 
Share this answer
 
Comments
Anshumaan Chaturvedi 14-Aug-14 14:50pm    
thanks, it worked. So irrespective of what the root node is, it will eventually find the descendant node. Am i correct?
Richard Deeming 14-Aug-14 14:51pm    
Yes - if the node you're looking for is anywhere underneath the root element, Descendants will find it.

If the node you're looking for might be the root node, then you'd use DescendantsAndSelf instead.
XElement findTag = xml.Elements("//Firmware_CRC").FirstOrDefault(); (Thanks, Richard)

I'd use a method that supports XPath -- for more flexibility when needed:

XElement findTag = xml.XPathSelectElements("//Firmware_CRC").FirstOrDefault();


"
// Selects nodes in the document from the current node that match the selection no matter where they are
"

http://www.w3schools.com/xpath/xpath_intro.asp[^]
 
Share this answer
 
v5
Comments
Anshumaan Chaturvedi 14-Aug-14 14:40pm    
IT's throwing :XMLException
Message:
Name cannot begin with the '/' character, hexadecimal value 0x5C.
PIEBALDconsult 14-Aug-14 14:53pm    
Alas, it doesn't support XPath; more reason not to use XElement.
I'm sure this came up here recently.
Richard Deeming 14-Aug-14 14:48pm    
The Elements method doesn't accept XPath queries. You would need to use the XPathSelectElements method[^] instead.
PIEBALDconsult 14-Aug-14 15:02pm    
That is such a basic function of XML, why would it not be built-in? The mind boggles. I always use XmlDocument.
Richard Deeming 14-Aug-14 15:06pm    
It is built-in. It's just not what the Elements method was intended to do.

If I pass a string containing an IP address to DateTime.Parse, it doesn't ping that IP address. That doesn't mean the DateTime.Parse is broken. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900