How do I take an XML file with nested elements and get a set of C# classes from it? -
How do I take an XML file with nested elements and get a set of C# classes from it? -
first off, i'm not terribly experienced in xml. know basics of reading in , writing it, part, things schemas start create eyes cross quickly. if looks i'm making wrong assumptions how xml works, there's chance am.
that disclaimer aside, problem i've run several times without finding agreeable solution. have xml defines data, including nested entries (to give example, file might have "power" element has kid node of "alternatepowers" in turn contains "power" elements). ideally, able generate quick set of classes xml file store info i'm reading in. general solution i've seen utilize microsoft's xsd.exe tool generate xsd file xml file , utilize same tool convert schema classes. grab is, tool chokes if there nested elements. example:
- column named 'power' belongs datatable: cannot set nested table name same name.
is there nice simple way this? did couple of searches similar questions here, questions found dealing generating schemas nested elements same name unanswered.
alternately, it's possible misunderstanding how xml , xsd work , it's not possible have such nesting...
update
as example, 1 of things i'd parse xml output of particular character builder program. fair warning, bit wordy despite me removing 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® , hero lab logo registered trademarks of lwd technology, inc. free download @ http://www.wolflair.com mutants & masterminds, sec edition ©2005-2011 greenish ronin publishing, llc. 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 enhancement non-effect trait, such ability (including saving throws) or skill (including attack or defense bonus). since toughness save cannot increased on own,use protection effect instead of enhanced toughness (see protection later in 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></usernotes><alternatepowers/><chainedpowers/><otherpowers/></power><power name="sailor suit (device 2)" info="" ranks="2" cost="8" range="" displaylevel="0" summary="hard lose" active="yes"><powerdesc>a device has 1 or more powers , can equipped , un-equipped.</powerdesc><descriptors/><elements/><options/><traitmods/><flaws/><powerfeats/><powerdrawbacks/><usernotes></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 harm. gain bonus on toughness saving throws equal protection rank.</powerdesc><descriptors/><elements/><options/><traitmods/><extras><extra name="impervious" info="" partialranks="2">your protection stops harm completely. if attack has harm bonus less protection rank, inflicts no harm (you automatically succeed on toughness saving throw). penetrating harm (see page 112) ignores modifier; must save against normally.</extra></extras><flaws/><powerfeats/><powerdrawbacks/><usernotes></usernotes><alternatepowers/><chainedpowers/><otherpowers/></power></otherpowers></power></powers></hero></document>
yes, there number of unnecessary tags in there, it's illustration of kind of xml i'd able plug in , reasonable. xml, when sent xsd, generates next error:
- column named 'traitmods' belongs datatable: cannot set nested table name same name.
i finished helping that. seek reading thread here: http://stackoverflow.com/a/8840309/353147
taking illustration , link, you'd have classes this.
public class powerfulness { xelement self; public power(xelement power) { self = power; } public alternatepowers alternatepowers { { homecoming new alternatepowers(self.element("alternatepowers")); } } } public class alternatepowers { xelement self; public alternatepowers(xelement power) { self = power; } public power2[] powers { { homecoming self.elements("power").select(e => new power2(e)).toarray(); } } } public class power2 { xelement self; public power2(xelement power) { self = power; } }
without knowing rest of xml, cannot create properties create each class/node level, should gist here , link.
you'd reference this:
power powerfulness = new power(xelement.load("file")); foreach(power2 power2 in power.alternatepowers.powers) { ... }
c# xml xsd
Comments
Post a Comment