force evaluation on string or character

G

Guest

Hi,
I am working with DOM and I need to do the following:

<sequence>
<element name="input1" type="string"/>
<element name="input2" type="string"/>
<element name="input3" type="string"/>
</sequence>


The problem is, I do not know how many inputs I will have until runtime. So
I have to generate the element Elements on the fly. One way to construct the
node would be like this:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<sequence>" +
"<element name='input1' type='string'/>" +
....and so on... +
"</seqence>");
MyDoc.ReplaceChild( doc, MyDoc.FirstChild ); or something along those lines

My question is, since I do not have a way of knowing how many inputs we have
until runtime, I have to put a for loop and name the inputs as input1,
input2....

How can I ge this 'input1' from my code which is doing this
for (int i = 0; i < numOfOperations; i++)
s = s + "<element name="+ 'input + i '+" type='string'/>");

I am getting 'input+i' instead of input1. Is there any way to force the
character to
convert the value instead of treating it as a literal??

Thanks
Faraz
 
G

Guest

Hi Faraz

You probably don't want to create your xml this way. Look at using
CreateElement and AppendChild instead...your way gives problems in terms of
hard to read string concatenation (as you have found!)...

s = s + "<element name="'input" + i + "' type='string'/>" should work
better...

HTH

Nigel Armstrong
 
N

Nick Malik

three things I'd suggest:
1) learn to use the DOM. You can create the child node and simply append it
to the list of nodes. No need for string manipulations like this.

2) take a look at the StringBuilder class. If you are going to concatenate
large numbers of strings, you will be very wasteful of memory, since strings
in .NET are immutable. This means that they do not change. In other words,
if you create a string, and append a single character, an entirely new
string is created, and both of the two operands are discarded.
StringBuilder doesnt' do this.

3) Your problem, below, can be fixed by moving a quote character around a
little. THe line:
s = s + "<element name="+ 'input + i '+" type='string'/>"); becomes
s = s + "<element name=input" + i +" type='string'/>");


--- Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top