XmlReadWrite

G

Guest

Hi,

Whats the best way to infile edit a value in an XML file? I want INI file
update functionality on a XML file.

This will come in 2.0 I was told, is this true? Until then how can I do
this easily today?


Thanks
 
C

Cor

Hi

In my opinion the most simple is to threat it as a dataset
dataset.readxml(path) and dataset.writexml(path)

I hope this helps,

Cor
 
J

Jon Skeet [C# MVP]

I was going to try reading from the file using StreamReader.ReadToEnd() then
pass that to XmlDoc.LoadXml(s); then parse it in memory and then write it
out again.

Any reason for using that rather than just
XmlDocument.Load(StreamReader) or XmlDocument.Load(string)?
 
G

Guest

I was going to try reading from the file using StreamReader.ReadToEnd() then
pass that to XmlDoc.LoadXml(s); then parse it in memory and then write it
out again.
 
G

Guest

Because it has XML in its name :D


I just need the easiest way to update a node in an XML file inplace.
 
J

Jon Skeet [C# MVP]

Because it has XML in its name :D

I just need the easiest way to update a node in an XML file inplace.

As you were doing: read it, modify the in-memory version, write it out
again. That's fine - I was just wondering why you were going to the
trouble of loading it yourself when it's easy to get the XmlDocument
class to do it.
 
C

Cor

I was going to try reading from the file using StreamReader.ReadToEnd()
then
pass that to XmlDoc.LoadXml(s); then parse it in memory and then write it
out again.
I think that if you are using Javascript for it that is the best method yes

A pity because with vb.net it is not more than (excluding the errortrapping)
if it is the first item in the first row roughly written
\\\
dim ds as dataset
ds.readxml(myfilepath)
ds.tables(0).rows(0)(0).item = "mychange"
ds.writexml(myfilepath)
///

Cor
 
G

Guest

Im now looking at the DataSet way as its easier to find a specific node than
the XmlDoc I think.
 
J

Jon Skeet [C# MVP]

Im now looking at the DataSet way as its easier to find a specific node than
the XmlDoc I think.

It depends on the structure of your data. Using XPath makes it fairly
easy to find a specific node, IMO. If your data naturally fits in a
dataset, that's fine, but certainly not all hierarchical data fits
neatly in a relational table form.
 
G

Guest

I basically want to update a node when I give it the following lookup form
"Node1.Node2.Node3.NodeToRead"

This is how i treat the XML like an ini file.
 
J

Jon Skeet [C# MVP]

I basically want to update a node when I give it the following lookup form
"Node1.Node2.Node3.NodeToRead"

This is how i treat the XML like an ini file.

Then you could use:

XmlNode node = doc.SelectSingleNode ("Node1/Node2/Node3/NodeToRead");
 
G

Guest

Any idea what the Regex expression to replace a "." with a "/" char?

I called th Regex ctor with "." and .Replace(.., "/") but every char gets
replaced.
 
J

Jon Skeet [C# MVP]

Any idea what the Regex expression to replace a "." with a "/" char?

I called th Regex ctor with "." and .Replace(.., "/") but every char gets
replaced.

Why bother with a regular expression? Just use
String.Replace(".", "/"). Is the format of the input absolutely fixed
though? It would be nicer to just state that XPath is the query format
to start with.
 
G

Guest

Yup, but I figured out the regex anyway :D.

Its fixed ok. Its period delimited so Ill use String.replace()
 

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