1

I'm a PHP programmer, and I'm trying to understand some code which I think is ASP.NET. This is also my first foray into XML. I don't have access to a Windows box to test on.

I need to produce XML output that third-party code can use. The third party wants to use our data instead of the data source they are currently using. I don't want to replicate the current XML structure exactly because it doesn't map well to our data.

The structure of the current XML is very flat. There are only a few nested elements and the third party doesn't make use of any of them. The third party does have a sub-contracted programmer, but he is very busy. Also, I want to understand, for myself, how this works.

This is an excerpt from a plugin for a custom CMS:

Dim obj_set As New Data.DataSet()
Using obj_reader As New System.Xml.XmlTextReader("http://www.example.com/xml_output.php")
  obj_set.ReadXml(obj_reader)
End Using
Dim obj_view As Data.DataView = obj_set.Tables("profile").DefaultView
obj_view.Sort = "cname"
Dim obj_data As Data.DataTable = obj_view.ToTable()

So from what I have gathered so far, this code

  • reads the XML file into a DataSet
  • sorts the profile table by cname
  • creates a new DataTable from the sorted view

There is other code that stores the new table to, and retrieves it from, cache. Then there is code that loops through the table rows and maps the column names to template variables.

Sample excerpt of current XML structure:

<profiles>
  <profile>
    <cname>ABC Corporation</cname>
    <fname>John</fname>
    <lname>Smith</lname>
    <sector>Widgets</sector>
    <subsectors>
      <subsector>Basic Widgets</subsector>
      <subsector>Fancy Widgets</subsector>
    </subsectors>
  </profile>
</profiles>

So what happens to the subsectors data? Does the reader create a separate table for it? If so, how are the tables related?

Our data includes multiple contacts per company. I could just create multiple elements at the top level fname1, fname2, fname3 to keep the flat structure. But I was thinking a nested structure makes sense for this kind of data. The problem is that I don't understand if such a structural change is compatible with the plugin code.

What kinds of changes would need to be made to the plugin code to make use of nested elements?

1 Answer 1

1

I was stumped on this myself, and I don't know if you still are, but for reference to others here's what I found.

You are right in assuming that the reader creates a separate table for it. Being that a DataSet can hold multiple tables, each "level" of elements gets its own table. However, any nested elements that have nested elements of their own will get their own table. Essentially, it keeps creating tables until it reaches the bottom of the xml tree. If an element has no children, it gets added as a cell in the data table.

In your case, dataSet.Tables[0] will hold the top level nodes (all the <.profiles>). But since the nested element <.profile> has elements of its own, Tables[0] will likely only have one row. The next level deeper, dataSet.Tables[1] will hold all <-profile> nodes. Although since <.subsectors> has sub-element <.subsector>, it will not be in Tables[1], but rather in Tables[2] which goes yet level deeper.

I know it has been a while since this was asked, but hopefully this will be helpful.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.