Hi,
How to bind the .xml data into mvvm and display in textblock in xaml.
I have to call REST API and API result in .xml and able to call api using below code -
Problem is how to bind to xml data into "mainviewmodel.cs" public function "loaddata"HTML Code:private void CallSamacharAPI() { WebClient apiparam = new WebClient(); apiparam.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; apiparam.UploadStringCompleted += new UploadStringCompletedEventHandler(Apiparam_UploadStringCompleted); string mRowsfrom = "0"; string mRowsNo = "10"; string mMenuId = "10"; string mValidkey = GetMD5Hash("SM@" + mRowsfrom + mRowsNo); StringBuilder postData = new StringBuilder(); postData.AppendFormat("{0}={1}", "rowsfrom", HttpUtility.UrlEncode(mRowsfrom)); postData.AppendFormat("&{0}={1}", "rowsno", HttpUtility.UrlEncode(mRowsNo)); postData.AppendFormat("&{0}={1}", "menuid", HttpUtility.UrlEncode(mMenuId)); postData.AppendFormat("&{0}={1}", "val_key", HttpUtility.UrlEncode(mValidkey)); var url = new Uri("http://api.development.com/v1/hometest/", UriKind.Absolute); apiparam.UploadStringAsync(url, "POST", postData.ToString()); } //Retrive XML result void Apiparam_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) { if (e.Result == null || e.Error != null) { MessageBox.Show("There was an error retriving Data!"); } else { var apiparam = sender as WebClient; apiparam.Headers["Content-type"] = "text/xml"; apiparam.UploadStringCompleted -= Apiparam_UploadStringCompleted; if (!e.Cancelled) { XDocument document = XDocument.Parse(e.Result); var xmlRowData = (from query in document.Descendants("RowData") select new SamacharDesh { Headline = (string)query.Element("Headline"), NewsID = (string)query.Element("News_ID"), Synopsis = (string)query.Element("news_body"), ImagePath = (string)query.Element("Image_Path") }).ToList(); } else MessageBox.Show("Operation Cancelled..."); } }
Please guide how to do it...HTML Code:public void LoadData() { SamacharDesh news_sd = new SamacharDesh(); // Sample data; replace with real data this.Items.Add(new ItemViewModel() { LineOne = "runtime one", LineTwo = "Maecenas praesent accumsan bibendum", LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu", LineFour ="Newly Added" }); this.Items.Add(new ItemViewModel() { LineOne = "runtime two", LineTwo = "Dictumst eleifend facilisi faucibus", LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus", LineFour = "Newly Added" }); this.Items.Add(new ItemViewModel() { LineOne = "runtime three", LineTwo = "Habitant inceptos interdum lobortis", LineThree = "Habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent", LineFour = "Newly Added" }); this.IsDataLoaded = true; }
Thanks
ASM


Reply With Quote