国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Windows8XLinq/XmlReader/Writer數(shù)據(jù)消息查詢

Windows8XLinq/XmlReader/Writer數(shù)據(jù)消息查詢

(2013-01-14 13:59:40)
標(biāo)簽:

雜談

    public sealed partialclass MainPage : Page
    {
       //首先獲取BingMaps Key. 進(jìn)入http://www.bingmapsportal.com/
       private string bingMapKey ="AnkX_4CsugRTUgfirUGvvEsSDd79n2PGur5p6DJ6JL6CbBOAM3VKGx3fTABgiKpx";

       public MainPage()
       {
          this.InitializeComponent();
       }

       private async void XLinqQuery_Click(objectsender, RoutedEventArgs e)
       {
           try
           {
              string cities = awaitProcessWithXLinq();
              this.msiTextBlock.Text =cities;
           }
           catch(System.Xml.XmlException ex)
           {
              this.msiTextBlock.Text ="Exception happened, Message:" + ex.Message;
           }
       }

       private async Task ProcessWithXLinq()
       {
           Uri uri =newUri(String.Format("http://dev.virtualearth.net/REST/v1/Locations?q=manchester&o=xml&key={0}",bingMapKey));

           HttpClientclient = new HttpClient();
          HttpResponseMessage response = await client.GetAsync(uri);
          response.EnsureSuccessStatusCode();

           Streamstream = await response.Content.ReadAsStreamAsync();

           XDocumentxdoc = XDocument.Load(stream);
           XNamespacexns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
           varaddresses = from node in xdoc.Descendants(xns + "Address")                          // querynode named "Address"
                        where node.Element(xns +"CountryRegion").Value.Contains("United States")   // where CountryRegion contains "UnitedStates"
                        select node.Element(xns +"FormattedAddress").Value;                     // select the FormattedAddress node'svalue

          StringBuilder stringBuilder = new StringBuilder("Manchester in US:");
           foreach(string name in addresses)
              stringBuilder.Append(name +"; ");

           returnstringBuilder.ToString();
       }

       private async void XmlQuery_Click(object sender,RoutedEventArgs e)
       {
           stringfilename = "manchester_us.xml";
           try
           {
              awaitProcessWithReaderWriter(filename);

              using (Stream s = awaitKnownFolders.PicturesLibrary.OpenStreamForReadAsync(filename))
              {
                 using (StreamReader sr = newStreamReader(s))
                 {
                    this.msiTextBlock.Text = sr.ReadToEnd();
                 }
              }
           }
           catch(UnauthorizedAccessException ex)
           {
              this.msiTextBlock.Text ="Exception happend, Message:" + ex.Message;
           }
       }

       private async TaskProcessWithReaderWriter(string filename)
       {
           Uri uri =newUri(String.Format("http://dev.virtualearth.net/REST/v1/Locations?q=manchester&o=xml&key={0}",bingMapKey));
           WebRequestrequest = WebRequest.Create(uri);

           // ifneeded, specify credential here
           //request.Credentials = new NetworkCredential();

          WebResponse response = await request.GetResponseAsync();
           StreaminputStream = response.GetResponseStream();

          XmlReaderSettings xrs = new XmlReaderSettings() { Async = true,CloseInput = true };
           using(XmlReader reader = XmlReader.Create(inputStream, xrs))
           {
              XmlWriterSettings xws = newXmlWriterSettings() { Async = true, Indent = true, CloseOutput =true };
              Stream outputStream = awaitKnownFolders.PicturesLibrary.OpenStreamForWriteAsync(filename,CreationCollisionOption.OpenIfExists);

              using (XmlWriter writer =XmlWriter.Create(outputStream, xws))
              {
                 string prefix = "";
                 string nameSpace = "";
                 await writer.WriteStartDocumentAsync();
                 await writer.WriteStartElementAsync(prefix,"Locations", nameSpace);

                 while (await reader.ReadAsync())
                 {
                     if(reader.NodeType == XmlNodeType.Element && reader.Name =="Address")
                     {
                        using (XmlReader subReader =reader.ReadSubtree())
                        {
                           bool isInUS = false;
                           while (await subReader.ReadAsync())
                           {
                               if(subReader.Name == "CountryRegion")
                               {
                                  string value = awaitsubReader.ReadInnerXmlAsync();
                                  if (value.Contains("UnitedStates"))
                                     isInUS = true;
                               }

                               if (isInUS&& subReader.NodeType == XmlNodeType.Element &&subReader.Name == "FormattedAddress")
                                  awaitwriter.WriteNodeAsync(subReader, false);//寫入操作: subReader->Stream
                           }
                        }
                     }
                 }

                 await writer.WriteEndElementAsync();
                 await writer.WriteEndDocumentAsync();
              }
           }

       }
    }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Async異步編程簡(jiǎn)介
全面解析C#中的異步編程
小心 HttpClient 中 FormUrlEncodeContent 的 bug
.NET4.5新功能:異步文件 I/O
[js] 第103天 你是如何更好地處理Async/Await的異常的?
XmlReader
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服