First off, I'm not terribly experienced in XML. I know the very basics of reading in and writing it, but for the most part, things like schemas start to make my eyes cross really quickly. If it looks like I'm making incorrect assumptions about how XML works, there's a good chance that I am.
That disclaimer aside, this is a problem I've run into several times without finding an agreeable solution. I have an XML which defines data, including nested entries (to give an example, a file might have a "Power" element which has a child node of "AlternatePowers" which in turn contains "Power" elements). Ideally, I would like to be able to generate a quick set of classes from this XML file to store the data I'm reading in. The general solution I've seen is to use Microsoft's XSD.exe tool to generate an XSD file from the XML file and then use the same tool to convert the schema into classes. The catch is, the tool chokes if there are nested elements. Example:
- A column named 'Power' already belongs to this DataTable: cannot set
a nested table name to the same name.
Is there a nice simple way to do this? I did a couple of searches for similar questions here, but the only questions I found dealing with generating schemas with nested elements with the same name were unanswered.
Alternately, it's also possible that I am completely misunderstanding how XML and XSD work and it's not possible to have such nesting...
Update
As an example, one of the things I'd like to parse is the XML output of a particular character builder program. Fair warning, this is a bit wordy despite me removing anything but the powers section.
<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<product name="Hero Lab" url="http://www.wolflair.com" versionmajor="3" versionminor="7" versionpatch=" " versionbuild="256">Hero Lab® and the Hero Lab logo are Registered Trademarks of LWD Technology, Inc. Free download at http://www.wolflair.com
Mutants & Masterminds, Second Edition is ©2005-2011 Green Ronin Publishing, LLC. All rights reserved.</product>
<hero active="yes" name="Pretty Deadly" playername="">
<size name="Medium"/>
<powers>
<power name="Enhanced Trait 16" info="" ranks="16" cost="16" range="" displaylevel="0" summary="Traits: Constitution +6 (18, +4), Dexterity +8 (20, +5), Charisma +2 (12, +1)" active="yes">
<powerdesc>You have an enhancement to a non-effect trait, such as an ability (including saving throws) or skill (including attack or defense bonus). Since Toughness save cannot be increased on its own,use the Protection effect instead of Enhanced Toughness (see Protection later in this chapter).</powerdesc>
<descriptors/>
<elements/>
<options/>
<traitmods>
<traitmod name="Constitution" bonus="+6"/>
<traitmod name="Dexterity" bonus="+8"/>
<traitmod name="Charisma" bonus="+2"/>
</traitmods>
<flaws/>
<powerfeats/>
<powerdrawbacks/>
<usernotes/>
<alternatepowers/>
<chainedpowers/>
<otherpowers/>
</power>
<power name="Sailor Suit (Device 2)" info="" ranks="2" cost="8" range="" displaylevel="0" summary="Hard to lose" active="yes">
<powerdesc>A device that has one or more powers and can be equipped and un-equipped.</powerdesc>
<descriptors/>
<elements/>
<options/>
<traitmods/>
<flaws/>
<powerfeats/>
<powerdrawbacks/>
<usernotes/>
<alternatepowers/>
<chainedpowers/>
<otherpowers>
<power name="Protection 6" info="+6 Toughness" ranks="6" cost="10" range="" displaylevel="1" summary="+6 Toughness; Impervious [4 ranks only]" active="yes">
<powerdesc>You're particularly resistant to harm. You gain a bonus on your Toughness saving throws equal to your Protection rank.</powerdesc>
<descriptors/>
<elements/>
<options/>
<traitmods/>
<extras>
<extra name="Impervious" info="" partialranks="2">Your Protection stops some damage completely. If an attack has a damage bonus less than your Protection rank, it inflicts no damage (you automatically succeed on your Toughness saving throw). Penetrating damage (see page 112) ignores this modifier; you must save against it normally.</extra>
</extras>
<flaws/>
<powerfeats/>
<powerdrawbacks/>
<usernotes/>
<alternatepowers/>
<chainedpowers/>
<otherpowers/>
</power>
</otherpowers>
</power>
</powers>
</hero>
</document>
Yes, there are a number of unnecessary tags in there, but it's an example of the kind of XML that I'd like to be able to plug in and get something reasonable. This XML, when sent into XSD, generates the following error:
- A column named 'traitmods' already belongs to this DataTable: cannot set
a nested table name to the same name.