简体   繁体   English

Xml解析并绑定到Xamarin表单上的ListView

[英]Xml parse and binding to listview on xamarin forms

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. 嗨,我正在尝试在我的xamarin.forms应用程序中解析XML.xml数据包含一个transactionID和几个具有多个答案的问题。 What Iam trying to achieve is Bind the questions in to label and and answers into dropdowns in a listview. Iam想要实现的是将问题绑定到标签中,并将答案绑定到列表视图的下拉列表中。

The XML Iam getting from API 从API获取XML Iam

<?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>

How Iam parsing it 我如何解析它

 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

My Data Model 我的数据模型

   public class XmlData
     {
          public string TransactionId { get; set; }
          public string Question { get; set; }
          public string Answer { get; set; }        
     }

My List View 我的列表视图

                <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) For your first question, 1)对于第一个问题,

Below are the class models to parse your xml, 下面是解析您的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; }
}

And by using LINQ to XML you can parse your xml to above class models like, 通过使用LINQ to XML,您可以将xml解析为上述类模型,

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) For your second question, 2)关于第二个问题,

Now you can create one ObservableCollection for above query result and bind it to list view in your xamarin form like 现在,您可以为上述查询结果创建一个ObservableCollection并将其绑定到xamarin表单中的列表视图,例如

ObservableCollection<Tranzaction> tranzactionsOC = new ObservableCollection<Tranzaction>(transactions);

Now tranzactionsOC is your ObservableCollection and you can bind it to your xamarin form 现在tranzactionsOC是您的ObservableCollection ,您可以将其绑定到您的xamarin表单

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

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