Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# RegEx
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:
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
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:
 
<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 14-Aug-14 4:02am
Edited 14-Aug-14 4:34am
digimanus30.9K
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
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
  Permalink  
Comments
Anshumaan Chaturvedi at 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 at 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 at 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 at 14-Aug-14 15:57pm
   
My mistake, did not see correct at the xml. Firmware_Crc is a child of Summary.
Anshumaan Chaturvedi at 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)

  Print Answers RSS
0 DamithSL 280
1 BillWoodruff 170
2 Zoltán Zörgő 170
3 OriginalGriff 134
4 Sergey Alexandrovich Kryukov 120
0 OriginalGriff 7,760
1 DamithSL 5,874
2 Sergey Alexandrovich Kryukov 5,444
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 14 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100