tags:

views:

573

answers:

2

This is my XML file:

<?xml version="1.0" standalone="no" ?> 
<!-- This file represents The Details of the user and the responces: --> 
<Survey>
  <Clientdetails>
    <ClientName xmlns="yash" /> 
    <ClientCompanyName xmlns="lnt" /> 
    <ClientTelNo. xmlns="546" /> 
    <ClientMobileNo xmlns="56" /> 
    <ClientEMail xmlns="56" /> 
  </Clientdetails>
  <ClientResponces>
    <Question1>
      <responce>1</responce> 
    </Question1>
    <Question2>
      <responce>2</responce> 
    </Question2>
    <Question3>
      <responce>3</responce> 
    </Question3>
    <Question4>
      <responce>3</responce> 
    </Question4>
    <Question5>
      <Question5.1>
        <responce>3</responce> 
      </Question5.1>
      <Question5.2>
        <responce>3</responce> 
      </Question5.2>
      <Question5.3>
        <responce>2</responce> 
      </Question5.3>
      <Question5.4>
        <responce>3</responce> 
      </Question5.4>
      <Question5.5>
        <responce>3</responce> 
      </Question5.5>
      <Question5.6>
        <responce>3</responce> 
      </Question5.6>
      <Question5.7>
        <responce>3</responce> 
      </Question5.7>
      <Question5.8>
        <responce>2</responce> 
      </Question5.8>
      <Question5.9>
        <responce>1</responce> 
      </Question5.9>
      <Question5.10>
        <responce>2</responce> 
      </Question5.10>
      <Question5.11>
        <responce>0</responce> 
      </Question5.11>
      <Question5.12>
        <responce>0</responce> 
      </Question5.12>
      <Question5.13>
        <responce>0</responce> 
      </Question5.13>
      <Question5.14>
        <responce>0</responce> 
      </Question5.14>
      <Question5.15>
        <responce>0</responce> 
      </Question5.15>
      <Question5.16>
        <responce>0</responce> 
      </Question5.16>
      <Question5.17>
        <responce>0</responce> 
      </Question5.17>
    </Question5>
  </ClientResponces>
</Survey>

I want to read this data into a DataSet. I want just the responses given by the client, and for 5th question I want the average of its 17 sub-questions.

For example, the DataSet should only contain this:

1
2
3
3
4

How to do this in C#?

+1  A: 

Try

 XmlDataDocument doc = new XmlDataDocument();
doc.LoadXml(<your string>);
DataSet ds = doc.DataSet;

You can then use LINQ to select distinct from the document.

update

 foreach (XmlNode node in doc.SelectNodes("\\Survey\ClientResponses"))
  {
     string text = node.InnerText;
  }

(not tested! your nested cases will be tricky!)

Dead account
Re your comment - he can't load **that** xml into a dataset because it is so horribly, horribly borked.
Marc Gravell
True... it's a bit of a mess.
Dead account
+3  A: 

That xml is pretty... horrible. My first suggestion would be to run it through an xslt transformation to get it into something more normal, that can be loaded with Load. Currently, it is abusing namespaces horribly.

Alternatively, forget DataSet, and parse it with XmlDocument or similar.

Here's an approach using XmlDocument (and using LINQ just for the average, to save a few lines of code):

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlElement el in doc.SelectNodes("//responce")) {
    Console.WriteLine(el.ParentNode.Name + "=" + el.InnerText);
}
XmlNodeList fiveAnswers = doc.SelectNodes(
      "/Survey/ClientResponces/Question5/*/responce");
double avg = fiveAnswers.Cast<XmlElement>()
     .Average(el => int.Parse(el.InnerText));
Console.WriteLine(avg);

For example of normal xml:

<question num="1">
  <response>1</response>
</question>
...
<question num="5">
  <response num="1">1</response>
  <response num="2">3</response>
  ...
</question>

or something comparable. Don't use the element name to infer identity.

Marc Gravell
-1. He can't load XML into a Dataset and you suggest XSLT? Yes, correct but does it help.
Dead account
+1. XmlDocument.SelectNodes(xpath) nicer ;)
Dead account
Not sure if you should include an "id" attribute that can contain duplicate values.
Tomalak
@Tomalak; better?
Marc Gravell
@Marc Gravell: Much, thank you. :-) (I know that as long as nothing else is defined (DTD/schema), an attribute named "id" has no special meaning in XML.)
Tomalak