我在变量中有以下标记。我需要使用C#将类型和 id 的值提取到不同的变量。什么是最好的方法?
<a href="gana:$type=FlexiPage;id=c828c4ea-075d-4dde-84f0-1876f8b71fa8;title=Workflow%20flexi$">workflow link</a>
答案 0 :(得分:3)
如果我必须解析HTML,我也会使用HtmlAgilityPack
。您可以使用SelectSingleNode
,GetAttributeValue
和字符串方法来创建键值和值对的字典:
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
答案 1 :(得分:2)
您可以在属性值上使用HTML Agility Pack和RegEx:
// 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");