views:

223

answers:

2

Here's the problem: my input is XML file that looks something like:

<BaseEntityClassInfo>
<item>
  <key>BaseEntityClassInfo.SomeField</key>
  <value>valueData1</value>
</item>
<item>
  <key>BaseEntityClassInfo.AdditionalDataClass.SomeOtherField</key>
  <value>valueData2</value>
</item>
<item>
  <key>BaseEntityClassInfo.AdditionalDataClass.AnotherClassInfo.DisplayedText</key>
  <value>valueData3</value>
</item>
...
...
</BaseEntityClassInfo>

The <key> element somehow describes entity classes fields and relationships (used in some other app that I don't have access to) and the <value> stores the actual data that I need.

My goal is to programatically generate a typed Dataset from this XML that could then be used for creating reports. I thought of building some XSD schema from input XML file first and then use this schema to generate Dataset but I'm not sure how to do that. The problem is that I don't want all data in one table, I need several tables with relationships based on the <key> value so I guess I need to infer relational structure from XML <key> data in some way. Are there other ways? Should I be using XSLT, Linq2XML, something else?

So what do you think? How could this be done and what would be the best approach?
Any advice, ideas, suggestions would be really appreciated!
Anyone? There's some additional info in comments bellow...

+1  A: 

Unfortunately, you won't really be able to use any XML tools to build your Dataset because the data you need isn't stored in XML format: it's stored in the format that "some other app" is using. If it looked like:

<BaseEntityClassInfo SomeField="valueData1">
  <AdditionalDataClass SomeOtherField="valueData2">
    <AnotherClassInfo DisplayedText="valueData3">
  </AdditionalDataClass>
</BaseEntityClassInfo>

Then you could use the standard XML handlers in .Net, but since it's not, the best you can do is pull the key and value out as pairs of strings and then write your own code to parse the key into the data entities you need (for example, split the key on periods, then recursively match or create data structures).

tloflin
There's a considerable gap between "doesn't match the default XML generated by DataSet" and "isn't data in XML format".
Jeffrey Hantin
+1  A: 

Without seeing all your data I'll have to guess, but it looks like the xml could be based on a class structure like:

using System;

[Serializable]
public class BaseEntityClassInfo
{
  public string SomeField {get; set;}
  public AdditionalDataClass _AdditionalDataClass {get; set;}

  public class AdditionalDataClass
  {
    public string SomeOtherField {get; set;}
    public AnotherClassInfo _AnotherClassInfo {get; set;}    
  }

  public class AnotherClassInfo
  {
    public string DisplayedText {get; set;}
  }

  public BaseEntityClassInfo BaseEntityClassInfoCreate()
  {
      BaseEntityClassInfo instance =
          new AdditionalDataClass
            {
                SomeField = "valueData1",
                _AdditionalDataClass =
                  new AdditionalDataClass
                      {
                          SomeOtherField = "ValueData2",
                          _AnotherClassInfo =
                              new AnotherClassInfo { DisplayedText = "valueData3" }
                      }
            };
      return instance;
  }

}

... and that the class structure is then serialized into XML.

This structure can be extended with your real data and if you end up with say

List<BaseEntityClassInfo> 

this could be the DataSource of your DataSet.

amelvin
you're right about class structure but what I need is to somehow construct xsd schema from this mess not just fill dataset with the data....
Mr. Brownstone