locked
XElement help for getting data RRS feed

  • Question

  • User-436309264 posted

    Hello Everyone,

    I have the following data in XML file . I want to retrive the data for the Answers i.e get all the answer 1 to 5

    <Question id="1" type='SingleChoice'>
    <Text>Where is your project primarily located? (select only one)</Text>
    <Answers>
    <Answer id="1"> In an urbanized area</Answer>
    <Answer id="2"> In a metropolitan area outside of core urbanized area </Answer>
    <Answer id="3"> In a small city/town</Answer>
    <Answer id="4"> In a rural area</Answer>
    </Answers>
    </Question>
    <Question id="2" type='SingleChoice'>
    <Text>What neighborhood is the project located in? (select only one)</Text>
    <Answers>
    <Answer id="1">Primarily a residential area</Answer>
    <Answer id="2">Primarily an employment area</Answer>
    <Answer id="3">Primarily an industrial/warehousing area</Answer>
    </Answers>
    </Question>

    I have defined the following class

    public class DiagnosticTool
    {
    public string Text { get; set; }

    public IList<string> Answers { get; set; }


    }

    I am using the following

    List<DiagnosticTool> diagnosticTools = (from el in root.Elements("Question")
    select new DiagnosticTool
    {
    Text = (string)el.Element("Text"),
    // Answers = (from ans in el.Elements("Answers")),
    }).ToList();

    My problem is how do I retrieve the answers

    Thanks

    Kapil 

    Tuesday, June 16, 2015 2:41 PM

Answers

  • User-271186128 posted

    Hi Kapil,

    How do I assigned the values to this custom class?

    As for this issue, you could use Attirbutes property to get the question Id and question type. You could refer to the following code:

                List<DiagnosticTool> queslist = new List<DiagnosticTool>();
                XmlNodeList list = xml.SelectNodes("/Questions/Question");
                foreach (XmlNode xn in list)
                {
                    DiagnosticTool dt = new DiagnosticTool();
                    dt.QuestionId = Convert.ToInt32(xn.Attributes["id"].Value); //Get Question Id                
                    dt.QuestionType = xn.Attributes["type"].Value;  //Get Question Type
                    dt.Text = xn["Text"].InnerText; //Get Text Node
    
                    List<Answer> answerlist = new List<Answer>();
                    XmlNodeList list2 = xn.SelectNodes(".//Answers/Answer");
                    foreach (XmlNode xn2 in list2)
                    {
                        Answer an = new Answer();
                        an.AnswerId = Convert.ToInt32(xn2.Attributes["id"].Value);//Get answer id.
                        an.Text = xn2.InnerText;//Get answer node.
    
                        answerlist.Add(an); //add answer to List
                    }
    
                    dt.Answers = answerlist;
    
                    queslist.Add(dt); // Add question to list
                }

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 17, 2015 9:51 PM

All replies

  • User-271186128 posted

    Hi Kapil,

    As for this issue, please try to use the following code.

    XML file:

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <Questions>
      <Question id="1" type='SingleChoice'>
        <Text>Where is your project primarily located? (select only one)</Text>
        <Answers>
          <Answer id="1"> In an urbanized area</Answer>
          <Answer id="2"> In a metropolitan area outside of core urbanized area </Answer>
          <Answer id="3"> In a small city/town</Answer>
          <Answer id="4"> In a rural area</Answer>
        </Answers>
      </Question>
      <Question id="2" type='SingleChoice'>
        <Text>What neighborhood is the project located in? (select only one)</Text>
        <Answers>
          <Answer id="1">Primarily a residential area</Answer>
          <Answer id="2">Primarily an employment area</Answer>
          <Answer id="3">Primarily an industrial/warehousing area</Answer>
        </Answers>
      </Question>
    </Questions>

    Code in page(.aspx.cs)

                XmlDocument xml = new XmlDocument();
                xml.Load(Server.MapPath("~/XMLFile1.xml"));
                StringBuilder sb = new StringBuilder();
    
                XmlNodeList list = xml.SelectNodes("/Questions/Question");
                foreach (XmlNode xn in list)
                {
                    sb.AppendLine(xn["Text"].InnerText); //Get Text Node
                    sb.AppendLine("<br />");
                    XmlNodeList list2 = xn.SelectNodes(".//Answers/Answer");
    
                    foreach (XmlNode xn2 in list2)
                    {
                        sb.AppendLine(xn2.InnerText); //Get answer node.
                        sb.AppendLine("<br />");
                    }
                    sb.AppendLine("<br />");
                }
                Response.Write(sb.ToString());

    output:

    Where is your project primarily located? (select only one) 
     In an urbanized area 
     In a metropolitan area outside of core urbanized area 
     In a small city/town 
     In a rural area 
    
     What neighborhood is the project located in? (select only one) 
     Primarily a residential area 
     Primarily an employment area 
     Primarily an industrial/warehousing area 

    Best Regards,
    Dillion

    Wednesday, June 17, 2015 5:06 AM
  • User-436309264 posted

    Thanks for the reponse. I have the following object defined

    public class DiagnosticTool
    {
    public string Text { get; set; }
    public IList<Answer> Answers { get; set; }
    public int QuestionId { get; set; }
    public string QuestionType { get; set; }
    }

    public class Answer
    {
    public int AnswerId { get; set; }
    public string Text { get; set; }
    }

    How do I assigned the values to this custom class?

    Thanks

    Kapil 

    Wednesday, June 17, 2015 10:23 AM
  • User-271186128 posted

    Hi Kapil,

    How do I assigned the values to this custom class?

    As for this issue, you could use Attirbutes property to get the question Id and question type. You could refer to the following code:

                List<DiagnosticTool> queslist = new List<DiagnosticTool>();
                XmlNodeList list = xml.SelectNodes("/Questions/Question");
                foreach (XmlNode xn in list)
                {
                    DiagnosticTool dt = new DiagnosticTool();
                    dt.QuestionId = Convert.ToInt32(xn.Attributes["id"].Value); //Get Question Id                
                    dt.QuestionType = xn.Attributes["type"].Value;  //Get Question Type
                    dt.Text = xn["Text"].InnerText; //Get Text Node
    
                    List<Answer> answerlist = new List<Answer>();
                    XmlNodeList list2 = xn.SelectNodes(".//Answers/Answer");
                    foreach (XmlNode xn2 in list2)
                    {
                        Answer an = new Answer();
                        an.AnswerId = Convert.ToInt32(xn2.Attributes["id"].Value);//Get answer id.
                        an.Text = xn2.InnerText;//Get answer node.
    
                        answerlist.Add(an); //add answer to List
                    }
    
                    dt.Answers = answerlist;
    
                    queslist.Add(dt); // Add question to list
                }

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 17, 2015 9:51 PM