Reputation: 41128
Given this XML, how can I retrive the HEX color?
<group>
<span style="color:#DF0000; font-style: italic; font-weight: bold">Webmaster</span>
</group>
I need to retrieve everything inside of the style. Then I can use the String.Substring method with .IndexOf() to retrieve the color for my use.
Thank you for the help.
Incase anyone is curious this is what I ended up with:
XElement str = doc.XPathSelectElement("/ipb/profile/group");
string color = str.Element("span").Attribute("style").Value;
color = color.Substring(color.IndexOf('#'), 7);
return color;
Upvotes: 0
Views: 113
Reputation: 23157
I'm not sure what the rest of your document looks like, but hopefully this points you in the right direction.
var node = xdoc.Descendants("group").Descendants("span").FirstOrDefault();
string style = node.Attribute("style").Value;
string[] styleElements = style.Split(';');
var colorElements = from x in styleElements
where x.StartsWith("color", StringComparison.InvariantCultureIgnoreCase)
select x;
string colorElement = (string)colorElements.FirstOrDefault();
Upvotes: 0
Reputation: 887225
You can use LINQ-to-XML:
var elem = XElement.Parse(str);
var attr = elem.Element("span").Attribute("style").Value;
Note that if your HTML is not completely well-formed, you should consider using the HTML Agility Pack instead.
Upvotes: 2