0

私はXamarin iOS iPad Appの質問アプリを作成しています。 誰かがアプリケーションを開くと、カテゴリが表示され、次にカテゴリからサブカテゴリが開きます。サブカテゴリから質問のリストが開きます。 質問を開くと、選択肢のリストと質問の回答を追加するためのテキストボックスが表示された詳細ページが表示されます。XMLに値を保存属性/ XML要素アンケートの回答者の値

今、問題は詳細ページを開き、その特定の質問の回答を保存することです。リストからの回答と選択肢は、その質問に対してXMLファイルに保存する必要があります。

これをどのように達成できますか?

<?xml version="1.0" encoding="utf-16"?> 
<Categories xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <SubCategories> 
    <Question QuestionTitle="I am question101" SubCategories="100" CategoryId="facilityId"> 
     <Checklists_Items Checklist_ItemTitle="title101" Checklist_ItemId="999" Checklist_ItemQuestion="question101" /> 
    </Question> 
    </SubCategories> 
</Categories> 
+0

使用モデルとバインディング –

答えて

0

私は次のようにして、これを達成: そのクラスのプロパティ値を設定し、XMLに値を持つクラスのオブジェクトをシリアル化、値を保存するために必要なすべての必要なプロパティを持つクラスファイルを作成しますシリアル化されたXML文字列をXMLファイルに書き込む。

以下は、オブジェクトクラスのサンプルコードです。

using System; 
using System.Collections.Generic; 
using System.Xml.Serialization; 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[XmlTypeAttribute(AnonymousType = true)] 
//[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 

public partial class QuestionsCollection 
{ 
    private string _Question; 
    private string _Answer; 

    public QuestionsCollection() 
    { 
    } 

    /// <remarks/> 
    [XmlAttributeAttribute()] 
    public string Question 
    { 
     get 
     { 
      return this._Question; 
     } 
     set 
     { 
      this._Question = value; 
     } 
    } 

    /// <remarks/> 
    [XmlArrayItemAttribute("Questions", IsNullable = false)] 
    public string Answer 
    { 
     get 
     { 
      return this._Answer; 
     } 
     set 
     { 
      this._Answer = value; 
     } 
    } 
} 

オブジェクトクラスをシリアル化するためにコード:

string question = "This is first question?"; 
string answer = "Answer to first question"; 

var path1 = Path.Combine(Directory, _path); 
//Setting Values 
      var c = new QuestionsCollection { Question = question, Answer = answer }; 

//Serialization of Object Class.  
      var s = new XmlSerializer(typeof(QuestionsCollection)); 
      var sb = new StringBuilder(); 

      using (var writer = new StringWriter(sb)) 
      { 
       try 
       { 
        s.Serialize(writer, c); 
//Write Serialized String to file. 
        File.WriteAllText(path1, sb.ToString(), Encoding.UTF8); 

       } 
       catch (Exception e) 
       { 

       } 
      }