0

I'm using C# (.Net framework 4 with VS 2010) for reading a dynamically generated XML file. The file is containing answers for questions (MCQ radio buttons, MCQ multi answer check boxes and simple text fields). Question IDs and option IDs are generated from database.

I just need to extract Question ID and relevent Answer ID(s). Sample XML is as follows.

<?xml version="1.0"?>
<Root>
   <!-- Radio Button Answers -->    
   <Question_6_Option>26</Question_6_Option>
   <Question_8_Option>32</Question_8_Option>
   <Question_9_Option>off</Question_9_Option>
   <!-- Check Box Answers -->
   <Question_15_Option_41>41</Question_15_Option_41>
   <Question_15_Option_42>off</Question_15_Option_42>
   <Question_16_Option_43>43</Question_16_Option_43>
   <!-- Text Box Answers -->
   <Question_23_Text>London</Question_23_Text>
</Root>

Above XML is generated in the format,

Tag name format: Question_QuestionID_SomeLogic based on answer type (radio, multiple options or text box).

If a user unanswered for a question value will display as "off". Those no need to consider.

How can I get the Question ID and Answer value from C#?

Thanks,

Chatur

5
  • good sir, I do believe this is a duplicate for the following question. Please remember to search for a question before you post it. stackoverflow.com/questions/2947738/… Commented Nov 26, 2012 at 12:55
  • 4
    Not an anwser but who generate the xml? IMHO it's really bad formatted. If you are the programmer, you should consider to change the structure Commented Nov 26, 2012 at 12:56
  • you can regexp element's name, but in general that's the wrong way to form xml, if possible, change that first :) Commented Nov 26, 2012 at 12:56
  • What have you tried so far ? Instantiating an XDocument, then enumerate all its XElement, running regexp over their names should do the trick. Commented Nov 26, 2012 at 13:01
  • When you think you've seen it all... In the check box answers what's up with the last number in the element? Commented Nov 26, 2012 at 13:01

1 Answer 1

0

NOT AN ANSWER, but included here rather than in the comments to make the XML more readable:

I'd suggest that if you're able to make your XML more computer readable - i.e. remove data from the tag names and put it into attributes to make it simpler to parse.

<?xml version="1.0"?>
<Root>
    <!-- Radio Button Answers -->    
    <Question number="6" type="radio"><Selected index="26" /></Question>
    <Question number="8" type="radio"><Selected index="32" /></Question>
    <Question number="9" type="radio" />
    <!-- Check Box Answers -->
    <Question number="15" type="check">
        <Selected index="41" />
        <Selected index="42" /><!-- not sure if this should be included, I didn't understand how question 15 was both off (unanswered) and 41 (answered) -->
    </Question>
    <Question number="16" type="check">
        <Selected index="43" />
    </Question>
    <!-- Text Box Answers -->
    <Question number="23" type="text">London</Question>
</Root>

As @NewAmbition stated above, the answer to your actual question is already elsewhere on this site.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.