Update a specific XML node

A

Alain R.

Hi,

I have the following XML file :
<?xml version='1.0' encoding='UTF-8'?>
<Database>
<ConnectionString Type="PostgreSQL v8.2.x">
<DBName>c2st</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>user</Username>
<Password>pwd</Password>
</ConnectionString>
<ConnectionString Type="MySQL v4.x">
<DBName>dbtest</DBName>
<Host>127.0.0.1</Host>
<Port>5432</Port>
<Username>myuser</Username>
<Password>mypwd</Password>
</ConnectionString>
</Database>

How can i update only 1 particular node of this XML file without
browsing the whole nodes ?

for example, i would like to change <Port> innerXML value (5432) to 1258
for MySQL v4.x connection string.

All methods i've read till now are browsing the whole nodes and this i
would like to avoid as much as possible.

thanks a lot,

Alain
 
J

Jon Skeet [C# MVP]

How can i update only 1 particular node of this XML file without
browsing the whole nodes ?

for example, i would like to change <Port> innerXML value (5432) to 1258
for MySQL v4.x connection string.

All methods i've read till now are browsing the whole nodes and this i
would like to avoid as much as possible.

What exactly do you mean by "browsing the whole nodes"? The easiest
way is probably to use an XPath expression to find the element you
want, and then update it. However, I don't know whether that would
count as "browsing the whole nodes" in your view.

If you want to update the file, you'll have to rewrite the whole thing
though.

Jon
 
A

Alain R.

i wanted to use this method but it does not work :

XPathDocument document = new XPathDocument(strFileName);
XPathNavigator navigator = document.CreateNavigator();

XPathNavigator nd =
navigator.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/DBName");

nd.SetValue(TBDBName.Text); <-- this does not work.

I've read that XPath is only for READ methods, but on internet a lot of
people use it to write...
so i'm confused now :-(

Al.
 
J

Jon Skeet [C# MVP]

i wanted to use this method but it does not work :

XPathDocument document = new XPathDocument(strFileName);
XPathNavigator navigator = document.CreateNavigator();

XPathNavigator nd =
navigator.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/DBName");

nd.SetValue(TBDBName.Text); <-- this does not work.

I've read that XPath is only for READ methods, but on internet a lot of
people use it to write...
so i'm confused now :-(

I don't tend to use XPathNavigator myself - I just call
SelectSingleNode or SelectNodes on XmlDocument. That way you get a
"normal" node back which you can change appropriately. I don't believe
it's quite as fast as XPathNavigator, but it's writable.

Jon
 
A

Alain R.

I did that (see below) and it works well...i'm just scared when XMl file
will be huge.

-----------------
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(strFileName);

XmlNode node =
xmlDoc.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/DBName");
node.InnerText = TBDBName.Text;

xmlDoc.Save(strFileName);
-------------

Al.
i wanted to use this method but it does not work :

XPathDocument document = new XPathDocument(strFileName);
XPathNavigator navigator = document.CreateNavigator();

XPathNavigator nd =
navigator.SelectSingleNode("/Database/ConnectionString[@Type='PostgreSQL
v8.2.x']/DBName");

nd.SetValue(TBDBName.Text); <-- this does not work.

I've read that XPath is only for READ methods, but on internet a lot of
people use it to write...
so i'm confused now :-(

I don't tend to use XPathNavigator myself - I just call
SelectSingleNode or SelectNodes on XmlDocument. That way you get a
"normal" node back which you can change appropriately. I don't believe
it's quite as fast as XPathNavigator, but it's writable.

Jon
 
J

Jon Skeet [C# MVP]

I did that (see below) and it works well...i'm just scared when XMl file
will be huge.

So how big do you expect your files to be, and have you tested it with
a file that large?

It's better to have evidence under your belt than to be scared of
something that may not be a problem.

Jon
 
A

Alain R.

for now XML is les than 1 kb as i just started to work on it.

but i expect it will reach 600 kb.
 
J

Jon Skeet [C# MVP]

for now XML is les than 1 kb as i just started to work on it.

but i expect it will reach 600 kb.

600K is still relatively small. By the time it's loaded into memory it
will be significantly bigger of course, but unless you're loading and
saving thousands of these on a very frequent basis, I wouldn't expect
it to be a problem.

To be confident, however, you should see if you can create or find a
file of that size, and see how it performs.

Jon
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Alain said:
for now XML is les than 1 kb as i just started to work on it.

but i expect it will reach 600 kb.

I think DOM in memory size typical is about 5 times on disk size.

In that case it is just 3 MB.

Arne
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
I think DOM in memory size typical is about 5 times on disk size.

In that case it is just 3 MB.

That will *very* much depend on what's in the document though.
Something with a lot of small attributes and elements will grow much
more than a document with a single element containing a huge amount of
text. The only test that makes sense is to try to find a typical
document *of the sort that the OP will be loading*.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jon said:
That will *very* much depend on what's in the document though.
Something with a lot of small attributes and elements will grow much
more than a document with a single element containing a huge amount of
text. The only test that makes sense is to try to find a typical
document *of the sort that the OP will be loading*.

True, but you are missing the point.

It does not really matter whether is is 5 times, 3 times or
7 times.

It is not a 100 times.

The original posters document is suited for DOM.

Arne

PS: The original poster actually posted some example XML in the
first post and it is about 3 times.
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
True, but you are missing the point.

It does not really matter whether is is 5 times, 3 times or
7 times.

It is not a 100 times.

So have you found the worst cases and measured it? There are any number
of things which could make it better or worse.
The original posters document is suited for DOM.

I suspect so too - but it would be best to check it.
PS: The original poster actually posted some example XML in the
first post and it is about 3 times.

You're assuming that *all* of the XML is like the sample posted. 600K
just of database connections is an awful lot - maybe there'll be other
data...
 
J

Jon Skeet [C# MVP]

So have you found the worst cases and measured it? There are any number
of things which could make it better or worse.

It's actually quite fun doing this - I've just written a small test
app to load an XML document and display the memory usage, trying to
maximise memory/file size. So far I've got just over 17...

Jon
 
M

Marc Gravell

So far I've got just over 17...

I'd be intersted in any conclusions / numbers you come up with... I've
typically used 10 as a guestimate of typical documents, so 17 is at
least in the same order. I'll take a guess that you're using lots of
unique short-name, short-value elements / attribs, i.e.
<a b="c" d="e" f="g"><h i="j">k</h><l/><m/></a>
(and clearly UTF8 to minimize physical file size)

Any repeatable numbers you come up with would be a useful brick to hit
people with when they come up with a design that involves loading a
10Mb xml file into XmlDocument on a web-server...

It isn't quite the same, but I've seen similar monstrosities using
OPENXML in SqlServer 2000 - then "why doesn my server keep dying?".
(As a minor caveat, large xml documents can work very well in
SqlServer 2005 using the xml data-type which invisibly shreds the data
into relational storage).

Marc
 
J

Jon Skeet [C# MVP]

I'd be intersted in any conclusions / numbers you come up with... I've
typically used 10 as a guestimate of typical documents, so 17 is at
least in the same order. I'll take a guess that you're using lots of
unique short-name, short-value elements / attribs, i.e.
<a b="c" d="e" f="g"><h i="j">k</h><l/><m/></a>
(and clearly UTF8 to minimize physical file size)

Nearly:

<a b="c">x</a>

That way you get a text node cheaply and force an attribute collection
cheaply.

I don't know if adding namespace references in there would make life
nastier. Likewise using a different character for each element would
probably hurt, but not a huge amount.
Any repeatable numbers you come up with would be a useful brick to hit
people with when they come up with a design that involves loading a
10Mb xml file into XmlDocument on a web-server...

Well, it's worth pointing out that I've been deliberately trying to
find a worst case here. I'd expect most situations to be *much* more
memory-friendly.

Jon
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jon said:
So have you found the worst cases and measured it?

No - I don't really see any point in it.
I suspect so too - but it would be best to check it.


You're assuming that *all* of the XML is like the sample posted. 600K
just of database connections is an awful lot - maybe there'll be other
data...

Sure.

But almost by definition I would expect that to be more close
to average than the worst case possible to create.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Marc said:
I'd be intersted in any conclusions / numbers you come up with... I've
typically used 10 as a guestimate of typical documents,

I would say that 10 is a very better-safe-than-sorry factor.

For real world XML.

Arne
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
No - I don't really see any point in it.

If the worst case is known to be okay for the OP's situation, then
there's no need to try a representative file. Otherwise, however, it
*is* a good idea to try one.
Sure.

But almost by definition I would expect that to be more close
to average than the worst case possible to create.

True - but without knowing how bad the worst case is, that's not much
comfort.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jon said:
True - but without knowing how bad the worst case is, that's not much
comfort.

Why not. If real XML files are <10 it does not matter much if is
possible to construct an example with 17 or 27.

Arne
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
Why not. If real XML files are <10 it does not matter much if is
possible to construct an example with 17 or 27.

But the point is that we didn't know what the genuine XML file would be
like. We hadn't experimented to find out *either* what it would be like
with real data *or* what a worst case would be. Using samples of other
XML files which may or may not be anything like the real one isn't
enough, IMO.
 

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