1

I have the below tag in a variable. I need to the extract the values of type and id to different variables using C#. What would be the best approach?

<a href="gana:$type=FlexiPage;id=c828c4ea-075d-4dde-84f0-1876f8b71fa8;title=Workflow%20flexi$">workflow link</a>
4
  • 1
    You could use Regex Commented Sep 8, 2017 at 11:12
  • @Lucifer you could. But it would be a really bad idea Commented Sep 8, 2017 at 11:12
  • Something presumably builds up that html tag - can't you get these values from the source. This looks awfully like an XY Problem to me. Commented Sep 8, 2017 at 11:13
  • @Jamiec No, I cannot actually. Somehow I need to extract from the above. Commented Sep 8, 2017 at 11:14

2 Answers 2

3

I would also use HtmlAgilityPack if i had to parse HTML. You can use SelectSingleNode, GetAttributeValue and string methods to create a dictionary of key- and value pairs:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html));
var anchor = doc.DocumentNode.SelectSingleNode("a");
string href = anchor.GetAttributeValue("href", "");

// take the text between both $
int startIndex = href.IndexOf('$') + 1;
href = href.Substring(startIndex, href.Length - startIndex); 

Dictionary<string, string> pageInfos = href.Split(';')
    .Select(token => token.Split('='))
    .ToDictionary(kv => kv[0].Trim(), kv => kv[1].Trim(), StringComparer.InvariantCultureIgnoreCase);
string id = pageInfos["id"];     // c828c4ea-075d-4dde-84f0-1876f8b71fa8
string type = pageInfos["type"]; // FlexiPage
Sign up to request clarification or add additional context in comments.

1 Comment

This is working like a charm with the HtmlAgilityPack. Thank you very much for the fast response.
2

You may use HTML Agility Pack and RegEx on the attribute value:

// With XPath   
var hrefValue = doc.DocumentNode
    .SelectNodes("//a")
    .First()
    .Attributes.First(a => a.Name =="href");

// With LINQ    
var hrefAttributeValue = doc.DocumentNode.Descendants("a")
    .Select(y => y.Descendants()
    .First().Attributes.First(a => a.Name =="href");

2 Comments

Doesn't show how to extract the id and type which is what OP wanted originally ;-)
You are right :-) Just thought that it would be too trivial. Your answer is better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.