试图理解C#中的XML序列化

时间:2014-08-18 16:10:39

标签: c# .net web-services xml-serialization mechanicalturk

我正在开展亚马逊机械土耳其人调查项目。我需要帮助的是理解下面的类在序列化时如何转换为XML。

public class ContentType
{
    public ContentType();

    [XmlChoiceIdentifier("ItemsElementName")]
    [XmlElement("Application", typeof(ApplicationContentType))]
    [XmlElement("Binary", typeof(BinaryContentType))]
    [XmlElement("FormattedContent", typeof(String))]
    [XmlElement("Text", typeof(String))]
    [XmlElement("Title", typeof(String))]

    public object[] Items { get; set; }

    [XmlElement("ItemsElementName")]
    [XmlIgnore]
    public ItemsChoiceType[] ItemsElementName { get; set; }
}

我正在尝试使用我的c#程序生成以下XML文档。

<QuestionForm xmlns="[the QuestionForm schema URL]">
  <Overview>
    <Title>Game 01523, "X" to play</Title>
    <Text>
      You are helping to decide the next move in a game of Tic-Tac-Toe.  The board looks like this:
    </Text>
  </Overview>
  <Question>
    <QuestionContent>
      <Text>
        What are the coordinates of the best move for player "X" in this game?
      </Text>
    </QuestionContent>
  </Question>
</QuestionForm>

概述和问题元素都是“ContentType”类型的对象。

以下是我尝试纠正的不完整且不正确的c#代码。

using System;
using System.Collections.Generic;
using System.Text;
using Amazon.WebServices.MechanicalTurk;
using Amazon.WebServices.MechanicalTurk.Domain;
using Amazon.WebServices.MechanicalTurk.Advanced;

namespace CreateHITProgram
{
    class FirstHIT
    {
        static void Main(string[] args)
        {
            // Create a HIT with the specified parameters.
            SimpleClient client = new SimpleClient();

            QuestionForm qnForm = new QuestionForm();
            ContentType qnOverview=new ContentType();
            qnForm.Overview = qnOverview;
            QuestionFormQuestion[] testQuestion1 = new QuestionFormQuestion[1];
            testQuestion1[0] = new QuestionFormQuestion();
            testQuestion1[0].IsRequired = true;
            testQuestion1[0].QuestionIdentifier = "1";
            ContentType questionContentType=new ContentType();
            Object[] questionItems=new object[1];
            questionItems[0]=new object();
            questionItems[0]="Swaroop";
            questionContentType.Items = questionItems;
            ItemsChoiceType[] ict = new ItemsChoiceType[1];
            ict[0] = new ItemsChoiceType();
            ict[0] = ItemsChoiceType.Text;
            questionContentType.ItemsElementName = ict;
            List<QualificationRequirement> qrList = new List<QualificationRequirement>();

            QualificationRequirement qr=new QualificationRequirement();
            qr.Comparator=Comparator.EqualTo;
            qr.IntegerValue=10;
            qr.QualificationTypeId="00000000000000000040";
            qr.RequiredToPreview=false;
            qrList.Add(qr);
            string[] rsgHIT=new string[1];
            rsgHIT[0]="Minimal";
            decimal reward=0.05m;

            HIT hit = client.CreateHIT("3LGOWEX152I2Q5FUSEN2GZFT8112F3", "My Hopes HIT", "Almost Last Attempt", string.Empty, qnForm, reward, 600, 600, 600, 10, "Swarup", qrList, rsgHIT);

            // Write the new HIT Id to the console.
            Console.WriteLine("Created HIT: {0}", hit.HITId);

            Console.WriteLine("Hit Location: {0}", client.GetPreviewURL(hit.HITTypeId));
        }
    }
}
运行上面的代码时出现

错误消息:

  

未处理的异常:System.ArgumentException:无法序列化   问题表(不包含问题)          在Amazon.WebServices.MechanicalTurk.QuestionUtil.SerializeQuestionForm(QuestionForm)   形成)          在Amazon.WebServices.MechanicalTurk.SimpleClient.CreateHIT(String   hitTypeId,String title,String description,String keywords,   QuestionForm问题,Nullable 1 reward, Nullable 1 assignmentDurationInSeconds,Nullable 1 autoApprovalDelayInSeconds, Int64 lifetimeInSeconds, Nullable 1   maxAssignments,String requesterAnnotation,        List`1 qualificationRequirements,String [] responseGroup)

0 个答案:

没有答案