Problem with C# using XPathNavigator  
Author Message
slippyC





PostPosted: XML and the .NET Framework, Problem with C# using XPathNavigator Top

...or maybe I just don't know what I'm doing!  Which could be a very good possiblity.

Going to explain what the app is actually doing first.  I'm saving some file paths to an xml document.  It is pretty much like a config file. 

I can successfully save folder paths.  If I try to save file paths, then for some reason it just doesn't write the info to the xml document(ex: C:\Windows\NOTEPAD.exe). 

First I'm converting the string to unicode, then converting to Base64String, and saving.

for (int x = 0; x < listBox1.Items.Count; x++)

{

filestring = Convert.ToBase64String(UnicodeEncoding.Unicode.GetBytes(listBox1.Items[x].Text));

}

xnav.SetValue(filestring); //this is XPathNavigator

This has no problem doing folder paths, like said above.  I can send the returned string to a text box on the form, when it is a file path(not folder path) and paste it in the xml document.  It will read the file path fine from the xml document and put it in a listbox.

The example above, was just an example of putting 1 path in the list box and getting the value.  That was meant to keep the example short. 

Anyone have any idea what the deal is here

If you need more code please just ask.  Since it is saving folder paths, it just riddles me though.

 

Thanks in Advance!!!!!

 

 



.NET Development30  
 
 
Martin Honnen





PostPosted: XML and the .NET Framework, Problem with C# using XPathNavigator Top

Why do you think you need to base64 encode stuff And what exactly fails with your approach, do you get an error message, if so which one for which statement exactly

Here is a simple .NET 2.0/C# example that takes your example file name and inserts it into an XmlDocument instance using the editable XPathNavigator API, then saves the XmlDocument to a file and reads it back to read out the example file name again using the XPathNavigator API:

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.LoadXml( );

string filePath = ;

XPathNavigator navigator = xmlDocument.CreateNavigator();

navigator.SelectSingleNode( ).SetValue(filePath);

string xmlFile = ;

xmlDocument.Save(xmlFile);

xmlDocument.Load(xmlFile);

navigator = xmlDocument.CreateNavigator();

Console.WriteLine(navigator.SelectSingleNode( ).Value);



 
 
slippyC





PostPosted: XML and the .NET Framework, Problem with C# using XPathNavigator Top

Why am I converting it

Because I'm going to encrypt the information. I just left those steps out to keep it simple. I actually haven't put the encryption in the program yet, first I needed this to work.

Is there an error thrown

Nope, it just doesn't write it to the file.

I can throw as many folder paths as I want at it, but if I put just one file path it will not save. The only thing I can think of that would even be different between a folder and file path is the period before the extension. Like I said, I can send the returned string to a textbox on the form. I can copy and paste that string into the xml document and it will pull the file name out fine.


 
 
slippyC





PostPosted: XML and the .NET Framework, Problem with C# using XPathNavigator Top

More info...

Alright, I'm narrowing it down.

I tried just to send a string to the xml document, the

This worked with no problems.  It has to be something with the listbox, the way it is returning the string.  Is there something that I'm not realizing that could be happening there

 

I appreciate you getting back with me too. 

Have a good weekend....

***Update***

The string returned from OpenFileDialog seems to be the problem.  It will not put that string in the xml document.  Can someone shed some light on this


 
 
slippyC





PostPosted: XML and the .NET Framework, Problem with C# using XPathNavigator Top

Some code to demonstrate.

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog fDlg = ofDialog;

XmlDocument xconfig = new XmlDocument();

xconfig.Load("config.xml");

XPathNavigator xnav = xconfig.CreateNavigator();

if (fDlg.ShowDialog() == DialogResult.OK)

{

xnav.SelectSingleNode("CryoConfig/Clientdata/filedata").SetValue(fDlg.FileName);

textBox1.Text = fDlg.FileName;

xconfig.Save("config.xml");

}

}

***Note***

This is assuming you already have an xml document with these nodes.


 
 
Sergey Dubinets - MSFT





PostPosted: XML and the .NET Framework, Problem with C# using XPathNavigator Top

For XmlDocument it don't care is the string actual file path or not.

Can you provide command line repro (one function) with hard coded string (encoded or not) that actually doesn't work