当前位置: 动力学知识库 > 问答 > 编程问答 >

How would I correctly parse data from wordpress mySQL DB to xamarin forms c#?

问题描述:

I am using the Jetpack Wordpress Plugin, which allows allows a mobile app to connect to the websites MySQL database, the data request completes successfully however it pulls all the html code with the content, all I require are the paragraphs in the content field, this is how it displays:

As you can see the image and Title appear fine, only the content pulls all the wordpress html with required text paragragh in between.

XAML File with code behind:

<ListView x:Name="postListView" HasUnevenRows="true" SeparatorVisibility="None">

<ListView.ItemTemplate>

<DataTemplate>

<ViewCell>

<StackLayout Padding="10" Orientation="Vertical" BackgroundColor="#4c4c4c" Spacing="5">

<Image x:Name="listViewImage"

Aspect="AspectFill"

Source="{Binding featured_image_url}"

HorizontalOptions="Fill"

HeightRequest="200" />

<StackLayout VerticalOptions="StartAndExpand">

<Label

Text="{Binding title}"

FontSize="15"

FontAttributes="Bold"

TextColor="#fac80d"

HorizontalTextAlignment="Center"

LineBreakMode="WordWrap"/>

<Label x:Name="ContentLabel"

Text="{Binding content}"

FontSize="12"

TextColor="White"

HorizontalTextAlignment="Center"/>

</StackLayout>

</StackLayout>

</ViewCell>

</DataTemplate>

</ListView.ItemTemplate>

Code Behind set to pull 10 blog posts:

 public partial class NewsPage : ContentPage

{

private const string Url = "www.website.com";

WordPressClient client;

public NewsPage()

{

InitializeComponent();

client = new WordPressClient();

}

protected override async void OnAppearing()

{

base.OnAppearing();

var postlist = await client.GetPostList(Url, PostType.post, PostStatus.publish, 10, 0);

postListView.ItemsSource = postlist.posts_list;

}

}

Model:

[JsonProperty("title")]

public string title {get; set;}

[JsonProperty("content")]

public string content {get; set;}

[JsonProperty("featured_image")]

public string featured_image_url {get; set;}

网友答案:

I found a solution to this issue, for anyone who experiences this in their project - when pulling data from a WordPress website, it will get a HTML string of data along with the theme you're using custom code, this is why HTML Agility pack was not recognizing the data as HTML, because it was full of square brackets [].

To remove the unwanted code I used REGEX, to use REGEX :

using System.Text.RegularExpressions;

In your code you need to take your HTML content and clean it up with the following snippet:

var plainContent = Regex.Replace(wallItem.content, @"^?\[.*", ""); 

var cleanedContent = plainContent;

//The Webview Source HTML
HtmlWBS.Html = cleanedContent;
网友答案:

Use the HTMLAgilityPack to parse the HTML and extract the content that you want.

var doc = new HtmlDocument ();
doc.LoadHtml (content);
// use the HTML DOM to extract the data you need
var div = doc.GetElementbyId ("...");

This blog post discusses this approach in more detail.

分享给朋友:
您可能感兴趣的文章:
随机阅读: