I'm a student just learning xml and c#. I'm writing a program that will read in xml data, process it, and write it back to the xml. Pretty standard stuff. Specifically, the program is going to allow the user to create/generate ch开发者_Python百科aracters and add new types of information (clans, skills, bonuses, etc.) for the L5R tapletop RPG game, as requested from a friend of mine. It's a learning experience and will give me something to put in my portfolio when I'm done.
I'm creating the xsd's and xml's first, and will get past the pseudocode phase once that's finished.
In my XMLs, I'm doing something like...
<clan clID="clanX">
<clName>...</clname>
<bonus bnID="clXbonusX>
<bnName>...</bnName>
<modBy>someNum</modBy>
</bonus>
</clan>
Since I haven't the experience doing this, once I start actually programming, the ID strings are going to change based on the user inputting new clan and bonus names and such.
Now I'm thinking a loop to do my sorting/renaming. But since the X's need to be variable characters, how would I go about ensuring that only the X gets changed, and specifically increments from 9 to 10 and not the next up ASCII value?
I'm intending to use the id's to pull in the start of a new clan element, and then to denote specifically which bonus (as the number of bonuses can vary) to put into the variables I'll be creating to hold them and their value.
Basically, the program will check the name of the bonus, add it to 1 array, and increase it by X in a parallel array.
Thanks in advance for any help!
once I start actually programming the ID strings are going to change based on the user inputting new clan and bonus names and such.
IDs that can be changed aren't really IDs. It seems, from your comment to Shekhar_Pro's answer, that your thinking is that IDs need to be unique within the scope of an XML document. That's true if and only if you're using the ID constraint in your DTD or schema. ID constraints aren't really a very common use case, and I don't think this is one.
The bigger question is: do you really need IDs at all? Assuming, for instance, that the names of the clans and the traits aren't to change over time - new ones might be added, but AEG isn't going to change the old ones anytime soon - if your document looked like this:
<clan name="Scorpion">
<bonus trait="Honor" value="-1"/>
<bonus trait="Intelligence" value="+1"/>
</clan>
not only is your document more concise, you can find all of the bonus
elements for the Bayushi family with the XPath:
//clan[@name='Scorpion']/bonus
In your schema, you can indicate that clan names need to be unique by using:
<xsd:element name="clan" type="clanType">
<xsd:unique name="nameKey">
<xsd:selector xpath="*/clan"/>
<xsd:field xpath="@name"/>
</xsd:unique>
</xsd:element>
You may need to jigger with the XPath in the selector depending on where in your document the clan
elements live.
Inserting Element name in ID with actual data seems redundent.. you already know that clID
in clan
element is for clan
element so just put your X
instead of whole clanX
. same goes for bnID
element.
That way you will even be saved from extracting actual info (X
) from 'ID'
精彩评论