简体   繁体   English

如何在 UWP 中获取 WebView 的网页内容

[英]How to get web content of WebView in UWP

I have a WebView for visiting web page.我有一个用于访问网页的 WebView。 And I use below code to get the output html source code.我使用下面的代码来获取输出 html 源代码。

    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)
        {
        }
    }

The resulthtml, which is the web page's source code is below:结果html,即网页的源代码如下:

.................................html = <userkeyinfo><div xmlns="http://www.w3.org/1999/xhtml" style="color: blue; font-weight: normal; margin-left: -4em;">&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">userkeyinfo</span>&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">oneuser</span>&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">uid</span>&gt;</a>10041167847<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;">&lt;/<span style="color: rgb(153, 0, 0);">uid</span>&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">lastname</span>&gt;</a>Xue<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;">&lt;/<span style="color: rgb(153, 0, 0);">lastname</span>&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">firstname</span>&gt;</a>Tom<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;">&lt;/<span style="color: rgb(153, 0, 0);">firstname</span>&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">middlename</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">gender</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">agegroup</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">addresses</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">tels</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">emails</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">birthyear</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">birthmonth</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">birthday</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">bloodtype</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">career</span>/&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">version</span>&gt;</a>2<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;">&lt;/<span style="color: rgb(153, 0, 0);">version</span>&gt;</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;">&lt;<span style="color: rgb(153, 0, 0);">registerlang</span>&gt;</a>zh_CN<span xmlns="http://www.w3.org/1999/xhtml" style="color: blue;">&lt;/<span style="color: rgb(153, 0, 0);">registerlang</span>&gt;</span></registerlang><span xmlns="http://www.w3.org/1999/xhtml" class="block" style="color: blue; margin-left: -2em;">&lt;/<span style="color: rgb(153, 0, 0);">oneuser</span>&gt;</span></oneuser><span xmlns="http://www.w3.org/1999/xhtml" class="block" style="color: blue; margin-left: -2em;">&lt;/<span style="color: rgb(153, 0, 0);">userkeyinfo</span>&gt;</span></userkeyinfo>

And if I formatted it (use this online tool: https://htmlformatter.com/ ) and save it to 1.html.如果我对其进行了格式化(使用此在线工具: https : //htmlformatter.com/ )并将其保存为 1.html。 The I use web explorer to visit it, then it looks like below.我使用网络浏览器访问它,然后它看起来像下面。

在此处输入图片说明

My purpose is to extract data from the shown xml component, eg get last name Xue, first name Tom, etc.我的目的是从显示的 xml 组件中提取数据,例如获取姓 Xue、名字 Tom 等。

I guess there needs three steps :我想需要三个步骤

  1. Format the html string;格式化html字符串;
  2. Get web content (xml strings) from the formatted html;从格式化的 html 中获取 web 内容(xml 字符串);
  3. Extract xml component from xml whole strings, eg last name Xue从 xml 整个字符串中提取 xml 组件,例如姓 Xue

How to do it?怎么做?

My purpose is to extract data from the shown xml component我的目的是从显示的 xml 组件中提取数据

HTML itself is also a kind of XML, which can be parsed as XML. HTML 本身也是一种 XML,可以解析为 XML。 If you want to get the content in the firstname tag, you can use the following method:如果要获取firstname标签中的内容,可以使用以下方法:

private async Task ReadXml(StorageFile file)
{
    string htmlContent = await FileIO.ReadTextAsync(file);
    XDocument doc = XDocument.Parse(htmlContent);
    var firstNameList = doc.Root.Descendants(XName.Get("firstname"));
    if (firstNameList.Count() > 0)
    {
        string firstName = firstNameList.First().Value;
        firstName = Regex.Replace(firstName, @"<[^>]*>", "");
        // do other things...
    }
}

XDocument uses Linq to complete the node query. XDocument使用 Linq 来完成节点查询。 In addition, it can also read and write XML documents through XMLDocument .此外,它还可以通过XMLDocument读写 XML 文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM