473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Need advice on XML usage

Hello everyone,

I need some advice on which class to use when working with an XML file
in a specific way. Basically I am writing a program, that needs to
save it's state into a file, so it can be read when it starts up
(based on where it ended). It's just 3 elements, with a short text and
a number associated to it. Something like this:

<element><name>something</name><number>3</number></element>

The thing is, there are 3 different elements (based on the name) and
they can't be repeated (so one version of each only). I will also be
updating them repeatedly.

So I need a simple way of searching for the elements in the XML file
(to make sure they are there,and if not to add them) and updating
their values (don't forget, I need to search for the element tag, not
the name tag, which means after I find it I am working with child
elements).. I was thinking either XmlDocument class, or using ADO.NET,
since the XmlTextReader/Writer class seem to be a bit acquired in this
situation. But after trying to use XmlDocument, even that seems
acquired for such a little task, so what do you think about using
ADO.NET (I must admit, I haven't tried it yet so, would have to learn
the basics)?

Anyway, any comments are appreciated.
Regards,
Amadej.
Nov 16 '05 #1
5 1720

"Amadej" <Am************@leoss.si> wrote in message news:m4********************************@4ax.com...
Hello everyone,

I need some advice on which class to use when working with an XML file
in a specific way. Basically I am writing a program, that needs to
save it's state into a file, so it can be read when it starts up
(based on where it ended). It's just 3 elements, with a short text and
a number associated to it. Something like this:

<element><name>something</name><number>3</number></element>

The thing is, there are 3 different elements (based on the name) and
they can't be repeated (so one version of each only). I will also be
updating them repeatedly.

So I need a simple way of searching for the elements in the XML file
(to make sure they are there,and if not to add them) and updating
their values (don't forget, I need to search for the element tag, not
the name tag, which means after I find it I am working with child
elements).. I was thinking either XmlDocument class, or using ADO.NET,
since the XmlTextReader/Writer class seem to be a bit acquired in this
situation. But after trying to use XmlDocument, even that seems
acquired for such a little task, so what do you think about using
ADO.NET (I must admit, I haven't tried it yet so, would have to learn
the basics)?

Anyway, any comments are appreciated.
Regards,
Amadej.


You can use XmlDocument: it has a SelectSingleNode method that
accepts an XPath expression and gives out the first node found.
(Alternative, use SelectNodes to get all that apply)

XmlDocument xml = new XmlDocument();
xml.Load(theFile);
XmlNode myNameNode = xml.SelectSingleNode("//name");
Hans Kesting
Nov 16 '05 #2
That was what I originally tried, but after I get the node I lost the
ability to select a single node from that node (to extract the name
element only, for instance), and it seemed to be a bit of a overhead
to write code with correct error checking and moving trough all the
child nodes just for 3 elements.

From what I have tried now, I must admit I preferre working with
ADO.NET and reading/writing into a XML. The only thing I worry about
is it might not be the most efficient way (resource vise) of doing it,
seeming that this is an small (but important and frequent) task in my
application?

Amadej.

On Fri, 23 Jul 2004 09:24:42 +0200, "Hans Kesting"
<ne***********@spamgourmet.com> wrote:

You can use XmlDocument: it has a SelectSingleNode method that
accepts an XPath expression and gives out the first node found.
(Alternative, use SelectNodes to get all that apply)

XmlDocument xml = new XmlDocument();
xml.Load(theFile);
XmlNode myNameNode = xml.SelectSingleNode("//name");
Hans Kesting


Nov 16 '05 #3

"Amadej" <Am************@leoss.si> wrote in message news:6b********************************@4ax.com...
That was what I originally tried, but after I get the node I lost the
ability to select a single node from that node (to extract the name
element only, for instance), and it seemed to be a bit of a overhead
to write code with correct error checking and moving trough all the
child nodes just for 3 elements.

From what I have tried now, I must admit I preferre working with
ADO.NET and reading/writing into a XML. The only thing I worry about
is it might not be the most efficient way (resource vise) of doing it,
seeming that this is an small (but important and frequent) task in my
application?

Amadej.

On Fri, 23 Jul 2004 09:24:42 +0200, "Hans Kesting"
<ne***********@spamgourmet.com> wrote:

You can use XmlDocument: it has a SelectSingleNode method that
accepts an XPath expression and gives out the first node found.
(Alternative, use SelectNodes to get all that apply)

XmlDocument xml = new XmlDocument();
xml.Load(theFile);
XmlNode myNameNode = xml.SelectSingleNode("//name");
and then:
XmlNode mySpecificNode = xml.SelectSingleNode("//" + myNameNode.InnerXml);

to find some other node, based on that first result?
or did I miss something? What will your final xml look like?



Hans Kesting

Nov 16 '05 #4
Hm,... I'm a bit confused now as to what that will do.
If I understand correctly, that will move me one level deeper (into
the next node). But an XmlNode class has very limited functions
compared to a XmlDocument, as far as working with elements and their
values goes, at least from what I've saw. At least it is so for my Xml
structure. My name and number values are, in this case, linked to the
parent node name, meaning I have to be aware of the parent all the
time for it to make sense.

If I am going to use XmlDocument, the structure will be the following:
<elementOne><name>value</name><number>value</number></elementOne>
<elementTwo><name>value</name><number>value</number></elementTwo>
<elementThree><name>value</name><number>value</number></elementThree>

Simply because then I can use the .SelectSingleNode method with the
attribute "//elementOne", so I get that element, and then find the
<name> value and <number> value for that given element.

If I go with ADO.NET, which I'll probably do, I'll keep the structure
more "XML like".
<element><id>One</id><name>value</name><number>value</number></element>
<element><id>Two</id><name>value</name><number>value</number></element>
<element><id>Three</id><name>value</name><number>value</number></element>

Because there's very little coding in trying to find a row with id=One
and extract the name and number value.

I got the basic functions for working with a DataSet and my Xml
structure already written, what I need to do is to use them on a
Pocket PC and see how performance is (the program's for the PC and PPC
platform).
On Fri, 23 Jul 2004 13:36:13 +0200, "Hans Kesting"
<ne***********@spamgourmet.com> wrote:
and then:
XmlNode mySpecificNode = xml.SelectSingleNode("//" + myNameNode.InnerXml);

to find some other node, based on that first result?
or did I miss something? What will your final xml look like?

>
>
>Hans Kesting
>


Nov 16 '05 #5

"Amadej" <Am************@leoss.si> wrote in message news:bh********************************@4ax.com...
Hm,... I'm a bit confused now as to what that will do.
If I understand correctly, that will move me one level deeper (into
the next node). But an XmlNode class has very limited functions
compared to a XmlDocument, as far as working with elements and their
values goes, at least from what I've saw. At least it is so for my Xml
structure. My name and number values are, in this case, linked to the
parent node name, meaning I have to be aware of the parent all the
time for it to make sense.

If I am going to use XmlDocument, the structure will be the following:
<elementOne><name>value</name><number>value</number></elementOne>
<elementTwo><name>value</name><number>value</number></elementTwo>
<elementThree><name>value</name><number>value</number></elementThree>

OK, I misunderstood, I had a different sort of structure in my mind.
Simply because then I can use the .SelectSingleNode method with the
attribute "//elementOne", so I get that element, and then find the
<name> value and <number> value for that given element.

and after you are done with elementOne, you can select "//elementTwo"
from the xmldocument and process that, and so on...
Or you can start with the ChildNodes list, so you can process all "element*"
nodes in order, whatever their name.
If I go with ADO.NET, which I'll probably do, I'll keep the structure
more "XML like".
<element><id>One</id><name>value</name><number>value</number></element>
<element><id>Two</id><name>value</name><number>value</number></element>
<element><id>Three</id><name>value</name><number>value</number></element>

Because there's very little coding in trying to find a row with id=One
and extract the name and number value.
with XPath :
doc.SelectSingleNode("//element[id='One']/name").InnerText
(warning: directly accessing InnerText will fail if no node was found)

or for your first structure:

doc.SelectSingleNode("//elementOne/name").InnerText
(same warning applies)

I got the basic functions for working with a DataSet and my Xml
structure already written, what I need to do is to use them on a
Pocket PC and see how performance is (the program's for the PC and PPC
platform).

Note: if your code should also work on the compact framework you are limited:
there is no XPath there! (I don't know about ADO.Net)
The "ChildNodes" route is about the only option left: loop through the list
until you find the node you want, then dive deeper.
As long as you retain a reference to the document, you can always start
"fresh", at the top of the list.
Hans Kesting
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: danaf | last post by:
Dear all, (1)Is it possible to host PHP-based Web Portal + MySQL Database (e.g. PHPNuke) on MS Windows System? If yes which server software I need? (2)Compare PHP over JSP/ASP/CGI in...
11
by: ma740988 | last post by:
I'm perusing a slide with roughly 12 bullets spread across 3 pages. Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more specifically the bullet that states: " Avoid the STL unless...
2
by: LoopyNZ | last post by:
Hi there, I'm trying to sharpen up my commenting in my code, partly for my own sake and partly for any inheritors of my code. I'm particularly wondering what comments are recommended for the...
0
by: Joe Ross | last post by:
(Apologies in advance if there is a better forum for asking advice on this topic). Our ASP.NET application occasionally starts spitting out OutOfMemory exceptions. When this happens, the memory...
1
by: Larry Neylon | last post by:
Hi, I'm working on a VBScript application on IIS6 and I'm looking for some advice about the best way of replacing or improving session variable usage. The application is in a secure extranet...
1
by: derrick.signup | last post by:
Hello all; I apologize for being slightly off topic, but I have great respect for the knowledge in this group. I am about to begin developing a .NET application where reliability (i.e.,...
3
by: shapper | last post by:
Hello, I created a simple class as follows: Public Class HelloWorld Public Function SayMessage() As String Return "Hello World!" End Function
1
by: gooop | last post by:
I am new to perl. I am having trouble with this code line I wrote. On thef fourth line I keep getting this message when I go to run this. . Please any advice would help.I am using TextPad, and window...
9
by: azrael | last post by:
I am starting to work on a application and need some advice. I am planing to develop a desktop application which would have some usage, but also it should be able to comunicate to a web server...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.