by ref or by value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello,

I ran the following code

//////////////////

XmlDocument dom = new XmlDocument();

dom .Load(@"C:\BindingFile.xml");

string mystring = dom.SelectSingleNode(MyXpath).InnerText;

mystring = mystring.Substring(0, 2);

////////////////////////////////////////

first time around mystring contained the xpath selectsinglenode return the
word "sean".
the second time i ran the code the xpath selectsinglenode returns the word
"se".

It seems that the xml file is storing the substring and not the original.

how could this be happening - i thought when i xpathed the string out - and
then worked on it - the original would be left in the dom???

could some one explain it to me please.

Also is there a way to leave the original string "sean" in the xml document
untouched. and yet still work on it locally.

thanks very much
 
This code would not change the contents of the xml document on disk - which
is what is what it would had to have done if the second time this code ran
you got a different result.

There must be other thing going on in your application. If you think that is
not the case, cut and paste these 4 lines of code into a brand new console
application, and everything should be fine.
 
Hi Sean,

Although as I see in your code, mystring is declared just before using,
I think the code you post is just for illustration. The actual code
would be different. You might declared mystring as a class level
varialbe (note that variable name is case-sensitive in C#), or you
might wrote the substring back to the node somewhere in your program.

Thi - http://thith.blogspot.com
 
sean said:
I ran the following code

//////////////////

XmlDocument dom = new XmlDocument();

dom .Load(@"C:\BindingFile.xml");

string mystring = dom.SelectSingleNode(MyXpath).InnerText;

mystring = mystring.Substring(0, 2);

////////////////////////////////////////

Could you post a short but complete example which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon
 
ok this is what happens
i have an XmlDocument named binding
One of its nodes contains a big string.
In this big string at various points is database configuration data.
Basically i have a text box which captures database config information.
i need to open the xml file take out the big string.
Update the database parts with the info in the text
then put the whole string back together and update the relevant node with it.

what is happening is - when i take the big string out and take it apart -
manipulate it, i am finding that the xml document gets modified as well.


here is a section of the code
/////////////////////////////////////


//build up the xpath
insertXpath = this.receivePortXpath + "[@Name='" + portName + "']";

insertXpath += "/" + this.receiveSQLTransportTypeXpath;

//get out the big string from the dom "binding"
string sqlConfigurationData = =
binding.SelectSingleNode(insertXpath).InnnerText;
//get the start position for the initial catalog info
int startInitialCatalog = sqlConfigurationData.IndexOf("Initial Catalog=", 0);

//now get the start position of just the config data for initial catalog
startInitialCatalog = startInitialCatalog + "Initital Catalog=".Length;

//Now get thje string out
string initialCatalog = sqlConfigurationData.Substring(startInitialCatalog);

//work out the end of initial catalog data
int endInitialCatalog = initialCatalog.IndexOf(";");

//work out the length of the config data
int initialCatalogLength = startInitialCatalog + endInitialCatalog;

//Finally get the string out
initialCatalog = initialCatalog.Substring(0, initialCatalogLength);



Now if i run this code once and stop it executing here -
then the big string that i get out of the xmlDocument at the start it has
been chopped
 
here is a section of the code

For a start, that's not your real code - this statement wouldn't
compile:
string sqlConfigurationData = =
binding.SelectSingleNode(insertXpath).InnnerText;

For a second thing, I asked for a short but *complete* example.
Something we can run. Please read the link I posted before.
 
It seems there is a problem with the following line:

//work out the length of the config data
int initialCatalogLength = startInitialCatalog + endInitialCatalog;

//Finally get the string out
initialCatalog = initialCatalog.Substring(0, initialCatalogLength);

Because you get the string out of initialCatalog, why adding
startInitialCatalog to get initialCatalogLength.

You may need to change to:
int initialCatalogLength = endInitialCatalog; // don't add
startInitialCatalog

You should debug your code to avoid "off-by-one" error.

The way you name your variables is very confusing. You should not use a
clearly-named var for other purpose.

I suggest you use regular expression for this type of task. I give a
simple sample:
RegEx ex = new RegEx(@"(.*Initial Catalog=).*(;.*)");
string newConfigString = ex.Replace(sqlConfigurationData, "$1" +
theNewCatalog + "$2");
 
hi guys sorry for not replying sooner.

jon yes you were right i didnt post my "real" code, it was so long winded i
thought it was better to show a snapshot.

Anyway, I used truongs idea and implemented the solution using regex.

it really simplified the code.

thanks to both of you for your time.
 
sean rogers said:
hi guys sorry for not replying sooner.

jon yes you were right i didnt post my "real" code, it was so long winded i
thought it was better to show a snapshot.

It's fine to show "dummy" code - so long as it's code which compiles,
runs, and demonstrates the real problem you've got. If it has problems
your real code doesn't though, how are we meant to know which problems
are in the real code and which are in your example

Just a thought for next time :)
 

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

Back
Top