Click here to Skip to main content
15,887,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
string replacetext="<td title=\"Compatible versions: a29d.3efe\" class=\"critical hastooltip\">"


this is string type variable which holds a value from XML document. My scenario is as such:
this string is being used to match the tag element and extract it's value. Problem is that the part Compatible Version:a29d.3efe is not constant and so I can't hardcode it to give desired result,that is my required tag. I wanted to have a regular expression or something similar solution where it can recognise any data, mostly alphanumberic characters with special symbols, and then I should be able to extract it's value. How can i do it?
This is piece fo code that I can share:
C#
if (MFMStatusUrlSettings != null)
                {
                    string replaceText = "<td title=\"Compatible versions: a29d.fe3e\" class=\"critical hastooltip\">";
                    string toGetMatch = MFMStatusUrlSettings.Replace(replaceText, "<td>");
                    MatchCollection Collection = Regex.Matches(toGetMatch, @"<td>(?<Value>.*?)</td>");
                    Firmware_CRC = GetStrValueBetweenTags(Collection[5].ToString(), "<td>", "</td>");

                    deviceInfo.Description = Firmware_CRC;
                }

So, for replaceText I wanted to get the tag data irrespective of whatever is there in Compatible Version section
XML documents:For reference
HTML
Document 1: 
<Status>
<Summary>
<Uptime>05:14:38</Uptime>
<FastWD>00:02:50</FastWD>
<SlowWD>18:45:22</SlowWD>
<Configuration_Download>Success</Configuration_Download>
<Firmware_CRC title="Compatible versions: a29d.4adc" 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>2 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>


Document 2:

HTML
<Status>
<Summary>
<Uptime>05:58:56</Uptime>
<FastWD>00:02:06</FastWD>
<SlowWD>18:01:04</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>2 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>
Posted
Updated 14-Aug-14 3:34am
v3

1 solution

The fact, that it is a XML document. Then do not use RegEx, as it will likely fail.
Use LINQ to XML[^] it is easy to search and replace XML values.

Example
C#
XElement xml = XElement.Parse(stringWithXml);
XElement findTag = xml.Elements("Firmware_CRC").FirstOrDefault();

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


There you have found Firmware_CRC title="" and Firmware_CRC value
 
Share this answer
 
Comments
Anshumaan Chaturvedi 14-Aug-14 11:01am    
I am sorry but I didn't get the part :
XElement xml=XElement.Parse(stringWithXml);

this stringWithXml is what actually?
P.S. I am fairly new to .NET
Anshumaan Chaturvedi 14-Aug-14 13:18pm    
Hey, I have tried to get Firmware_Crc but the element is under another node .how do i resolve that?
Kim Togo 14-Aug-14 14:17pm    
If it is under another node, just append an extra .Elements("Summary")

Like

var firmware = xml.Elements("Summary").Elements("Firmware_Crc").FirstOrDefault()
Kim Togo 14-Aug-14 15:57pm    
My mistake, did not see correct at the xml. Firmware_Crc is a child of Summary.
Anshumaan Chaturvedi 14-Aug-14 15:58pm    
Thanks.

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