tags:

views:

243

answers:

2

I have an XML document as below:

<?xml version="1.0" encoding="utf-8" ?>
 <Providers>
    <Provider>
      <ProviderType>Email</ProviderType>
      <ProviderTitle>MyProviderTitle</ProviderTitle>
       <DeliveryRules>
          <PersonalDelivery>true</PersonalDelivery>
       </DeliveryRules>
      <ProviderConfiguration>
    <SendTo>
      <Address>myEmailAddress</Address>
      <Address>myEmailAddress</Address>
    </SendTo>
  </ProviderConfiguration>
</Provider>
    <Provider>
      <ProviderType>Email</ProviderType>
      <ProviderTitle>MyProviderTitle</ProviderTitle>
       <DeliveryRules>
          <PersonalDelivery>true</PersonalDelivery>
       </DeliveryRules>
      <ProviderConfiguration>
    <SendTo>
      <Address>myEmailAddress</Address>
      <Address>myEmailAddress</Address>
    </SendTo>
  </ProviderConfiguration>
</Provider>
</Providers>

Now when I pull this into a dataset using:

 DataSet dsConfiguration = new DataSet();
 dsConfiguration.ReadXml("myfile.xml"));

How can I iterate through providers?

A: 
foreach (var Provider in dsConfiguration.Tables["Provider"].Rows)
{
    // your code here
}
HuBeZa
So does that mean I need to create a custom provider class?
JL
You can use this code to access SendTo:Provider.GetChildRows("Provider_ProviderConfiguration")[0].GetChildRows("ProviderConfiguration_SendTo")but I agree with Henk, XDocument is more suitable.
HuBeZa
+1  A: 

When it comes to more hierarchical data, you're much better off deserializing to a strongly typed object graph.

Here is a very quick implementation that should work for your example document:

[XmlType("Providers")]
public class Providers : List<Provider> { }

public class Provider
{
    public string ProviderType { get; set; }
    public string ProviderTitle { get; set; }
    public DeliveryRules DeliveryRules { get; set; }
    public ProviderConfiguration ProviderConfiguration { get; set; }
}

public class DeliveryRules
{
    public bool PersonalDelivery { get; set; }
}

public class ProviderConfiguration
{
    [XmlArrayItem("Address")]
    public string[] SendTo { get; set; }
}

public static void Main()
{
    var serializer = new XmlSerializer(typeof (Providers));
    Providers providers;
    using (var stream = File.OpenRead("myfile.xml"))
    {
        providers = (Providers) serializer.Deserialize(stream);
    }
    foreach (var provider in providers)
    {
        Console.WriteLine(provider.ProviderTitle);
        foreach (var address in provider.ProviderConfiguration.SendTo)
        {
            Console.WriteLine("\t" + address);
        }
    }
}

Obviously this approach requires more plumbing work but if you can get your hands on an XSD that describes your document's format then you can automatically generate all your classes using the XSD.exe tool.

Nathan Baulch
Thanks I had this custom serialization implemented, but I decided I wanted something more dynamic, which is why I decided to try datasets... seems to me Datasets are just not the right tool for the job at all. There is a lot of hype in the marketplace about datasets and their ease of use, you often hear, just pop it in a dataset serialize, deserialize. the reality is, its not that simple!
JL