Need advice on XML usage


A

Amadej

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.
 
Ad

Advertisements

H

Hans Kesting

Amadej said:
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
 
A

Amadej

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.
 
H

Hans Kesting

Amadej said:
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.
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?
 
A

Amadej

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).
 
Ad

Advertisements

H

Hans Kesting

Amadej said:
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
 
Ad

Advertisements


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

Similar Threads

Need an advice 0
Need Advice 1
XML Document memory usage 3
Best Practice Advice XML 2
Need advice - Remoting 1
Reflection - Need advice 4
Need advice for Middleware 5
OOP advice needed 13

Top