问题描述
您好,我正在尝试在我的 xamarin.forms 应用程序中解析 XML.xml 数据包含一个 transactionID 和几个带有多个答案的问题.我想要实现的是将问题绑定到标签中,并将答案绑定到列表视图中的下拉列表中.
Hi I am trying to parse XML in my xamarin.forms app.The xml data contains one transactionID and couple of questions with multiple answers. What Iam trying to achieve is Bind the questions in to label and and answers into dropdowns in a listview.
从 API 获得的 XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<PlatformResponse>
<TransactionDetails>
<TransactId>39562</TransactionId>
</TransactionDetails>
<Response>
<Questions>
<Question type="1" text="Which one is correct?">
<Answer correct="false">test1</Answer>
<Answer correct="false">test2</Answer>
<Answer correct="false">test3</Answer>
<Answer correct="false">test4</Answer>
<Answer correct="false">test5</Answer>
<Answer correct="false">test5</Answer>
</Question>
<Question type="2" text="Which one is associated with you?">
<Answer correct="false">test1</Answer>
<Answer correct="false">test2</Answer>
<Answer correct="false">test3</Answer>
<Answer correct="false">test4</Answer>
<Answer correct="false">test5</Answer>
<Answer correct="false">test5</Answer>
</Question>
<Question type="3" text="Which one of the following is true ?">
<Answer correct="false">test1</Answer>
<Answer correct="false">test2</Answer>
<Answer correct="false">test3</Answer>
<Answer correct="false">test4</Answer>
<Answer correct="false">test5</Answer>
<Answer correct="false">test5</Answer>
</Question>
</Questions>
</Response>
</PlatformResponse>
我如何解析它
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ProtocolVersion = HttpVersion.Version10;
webRequest.Method = "POST";
webRequest.ContentType = "text/xml charset=utf8";
webRequest.ContentLength = postData.Length;
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
requestStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader reader = new StreamReader(webResponse.GetResponseStream());
string reader2 = reader.ReadToEnd();
List<XmlData> ObjXmlData = new List<XmlData>();
XDocument doc = XDocument.Parse(reader2);
// How Can I bind it to the listview
我的数据模型
public class XmlData
{
public string TransactionId { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
}
我的列表视图
<ListView x:Name="QuestionsListView" ItemsSource="{Binding}"
HasUnevenRows="True"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="•" FontSize="Medium" TextColor="Green" Margin="0,20,0,0"/>
<Label Text="{Binding Question}" FontSize="Small" TextColor="#474747" Margin="0,20,0,0">
</Label>
</StackLayout>
<StackLayout Orientation="Horizontal" Margin="10,0,10,0" HorizontalOptions="FillAndExpand" >
<Picker x:Name="picker1" Title="Select answer" ItemDisplayBinding="{Binding Answer}" HorizontalOptions="FillAndExpand" FontSize="Small" TextColor="Gray">
</Picker>
<Image Source="downarrow.png" HorizontalOptions="End" HeightRequest="20" WidthRequest="20" ></Image>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView
>
我如何将这些问题和答案绑定到选择器和标签.任何帮助表示赞赏.
How Can I bind these questions and answers to picker and label. Any help appreciated.
推荐答案
1) 对于您的第一个问题,
1) For your first question,
下面是解析你的xml的类模型,
Below are the class models to parse your xml,
class Answer
{
public string Text { get; set; }
public bool Correct { get; set; }
}
class Question
{
public string Ques { get; set; }
public string Type { get; set; }
public List<Answer> Answers { get; set; }
}
class Tranzaction
{
public string TransactionId { get; set; }
public List<Question> Questions { get; set; }
}
并且通过使用 LINQ to XML,您可以将您的 xml 解析为上述类模型,例如,
And by using LINQ to XML you can parse your xml to above class models like,
XDocument doc = XDocument.Parse("Your xml text here");
List<Tranzaction> transactions = (from p in doc.Descendants("PlatformResponse")
select new Tranzaction
{
TransactionId = p?.Elements("TransactionDetails")?.FirstOrDefault()?.Element("TransactionId")?.Value.Trim(),
Questions = (from q in p?.Descendants("Questions")?.Elements("Question")
select new Question
{
Ques = q?.Attribute("text")?.Value,
Type = q?.Attribute("type")?.Value,
Answers = (from a in q?.Elements("Answer")
select new Answer
{
Text = a?.Value,
Correct = Convert.ToBoolean(a?.Attribute("correct")?.Value)
}).ToList()
}).ToList()
}).ToList();
2)对于你的第二个问题,
现在您可以为上述查询结果创建一个 ObservableCollection
并将其绑定到您的 xamarin 表单中的列表视图
Now you can create one ObservableCollection
for above query result and bind it to list view in your xamarin form like
ObservableCollection<Tranzaction> tranzactionsOC = new ObservableCollection<Tranzaction>(transactions);
现在 tranzactionsOC
是您的 ObservableCollection
,您可以将其绑定到您的 xamarin 表单
Now tranzactionsOC
is your ObservableCollection
and you can bind it to your xamarin form
这篇关于Xml 解析并绑定到 xamarin 表单上的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!