C# 如何在UWP中获取WebView的web内容
我有一个访问网页的网络视图。我使用下面的代码来获取输出的html源代码C# 如何在UWP中获取WebView的web内容,c#,uwp,C#,Uwp,我有一个访问网页的网络视图。我使用下面的代码来获取输出的html源代码 private void Webview_Loaded(object sender, RoutedEventArgs e) { string htmlStr = LenovoID.GetProfileData(); webview.Navigate(new Uri(htmlStr)); } private async void Webview_Navigati
private void Webview_Loaded(object sender, RoutedEventArgs e)
{
string htmlStr = LenovoID.GetProfileData();
webview.Navigate(new Uri(htmlStr));
}
private async void Webview_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string resulthtml = null;
try
{
resulthtml = await webview.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
Debug.WriteLine(".................................html = {0}", resulthtml);
}
catch (Exception ex)
{
}
}
resulthtml是网页的源代码,如下所示:
.................................html = <userkeyinfo><div xmlns="http://www.w3.org/1999/xhtml" style="color: blue; font-weight: normal; margin-left: -4em;"><?xml version="1.0" encoding="ISO-8859-1"?></div><style xmlns="http://www.w3.org/1999/xhtml">@namespace html url(http://www.w3.org/1999/xhtml); :root { font:small Verdana; font-weight: bold; padding: 2em; padding-left:4em; } * { display: block; padding-left: 2em; } html|style { display: none; } html|span, html|a { display: inline; padding: 0; font-weight: normal; text-decoration: none; } html|a[tabindex='0'] { cursor: pointer; } html|span.block { display: block; } *[html|hidden], *[html|hidden] *, span.block[html|hidden] { display:none!important; } .expand { display: block; } .expand:before { content: '+'; color: red; position: absolute; left: -1em; } .collapse { display: block; } .collapse:before { content: '-'; color: red; position: absolute; left:-1em; } </style><a xmlns="http://www.w3.org/1999/xhtml" tabindex="0" class="collapse" role="treeitem" aria-expanded="true" aria-posinset="1" aria-setsize="1" aria-level="1" style="color: blue; margin-left: -2em; position: relative;"><<span style="color: rgb(153, 0, 0);">userkeyinfo</span>></a><oneuser><a xmlns="http://www.w3.org/1999/xhtml" tabindex="0" class="collapse" role="treeitem" aria-expanded="true" aria-posinset="1" aria-setsize="3" aria-level="2" style="color: blue; margin-left: -2em; position: relative;"><<span style="color: rgb(153, 0, 0);">oneuser</span>></a><uid><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="1" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">uid</span>></a>10041167847<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;"></<span style="color: rgb(153, 0, 0);">uid</span>></span></uid><lastname><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="2" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">lastname</span>></a>Xue<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;"></<span style="color: rgb(153, 0, 0);">lastname</span>></span></lastname><firstname><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="3" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">firstname</span>></a>Tom<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;"></<span style="color: rgb(153, 0, 0);">firstname</span>></span></firstname><middlename><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="4" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">middlename</span>/></a></middlename><gender><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="5" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">gender</span>/></a></gender><agegroup><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="6" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">agegroup</span>/></a></agegroup><addresses><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="7" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">addresses</span>/></a></addresses><tels><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="8" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">tels</span>/></a></tels><emails><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="9" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">emails</span>/></a></emails><birthyear><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="10" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">birthyear</span>/></a></birthyear><birthmonth><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="11" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">birthmonth</span>/></a></birthmonth><birthday><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="12" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">birthday</span>/></a></birthday><bloodtype><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="13" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">bloodtype</span>/></a></bloodtype><career><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="14" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">career</span>/></a></career><version><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="15" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">version</span>></a>2<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;"></<span style="color: rgb(153, 0, 0);">version</span>></span></version><registerlang><a xmlns="http://www.w3.org/1999/xhtml" tabindex="-1" role="treeitem" aria-expanded="true" aria-posinset="16" aria-setsize="16" aria-level="3" style="color: blue; margin-left: -2em;"><<span style="color: rgb(153, 0, 0);">registerlang</span>></a>zh_CN<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;"></<span style="color: rgb(153, 0, 0);">registerlang</span>></span></registerlang><span xmlns="http://www.w3.org/1999/xhtml" class="block" style="color: blue; margin-left: -2em;"></<span style="color: rgb(153, 0, 0);">oneuser</span>></span></oneuser><span xmlns="http://www.w3.org/1999/xhtml" class="block" style="color: blue; margin-left: -2em;"></<span style="color: rgb(153, 0, 0);">userkeyinfo</span>></span></userkeyinfo>
html=?xml version=“1.0”encoding=“ISO-8859-1”?命名空间html url(http://www.w3.org/1999/xhtml); :根{font:small-Verdana;字体大小:粗体;填充:2em;左填充:4em;}*{display:block;padding left:2em;}html |样式{display:none;}html | span,html | a{display:inline;padding:0;font-weight:normal;text-decoration:none;}html | a[tabindex='0']{cursor:pointer;}html | span.block{display:block;}*[html | hidden],*[html | hidden]*,span.block[html | hidden]{display:none!important;}。展开{display:block;}。展开:before{content:'+';颜色:红色;位置:绝对;左:-1em;}。折叠{display:block;}。折叠:在{content:'-'之前;颜色:红色;位置:绝对;左:-1em;})并将其保存到1.html。我使用web explorer访问它,然后它如下所示
我的目的是从显示的xml组件中提取数据,例如获取姓Xue、名Tom等
我想需要三个步骤:
格式化html字符串
从格式化的html获取web内容(xml字符串)
从xml整个字符串中提取xml组件,例如姓氏
怎么做
我的目的是从显示的xml组件中提取数据
HTML本身也是一种XML,可以解析为XML。如果要获取firstname
标记中的内容,可以使用以下方法:
private async Task ReadXml(存储文件)
{
字符串htmlContent=await FileIO.ReadTextAsync(文件);
XDocument doc=XDocument.Parse(htmlContent);
var firstNameList=doc.Root.substands(XName.Get(“firstname”);
if(firstNameList.Count()>0)
{
字符串firstName=firstNameList.First().Value;
firstName=Regex.Replace(firstName,@“]*>”,“”);
//做其他事情。。。
}
}
XDocument
使用Linq完成节点查询。此外,它还可以通过来读写XML文档。谢谢!我会试试你的代码。这是我第三个问题的答案。你对另外两个问题有什么想法吗?嗨,你可以通过XDocument
将解析和数据提取结合起来,我已经修改了我的谢谢!最后我使用http请求(GET)获取网页内容,然后使用您的方法获取xml内容。我建议您尝试使用另一种方法。您可以在该文档上执行任何JS代码。因此,您可以用JS编写提取函数,然后用c#解析其结果。谢谢!我已按您所说的更改了解决方案。