all groups > dotnet xml > september 2005 >
You're in the

dotnet xml

group:

Looking for a better way to handle errors


Looking for a better way to handle errors JJ
9/29/2005 8:57:48 AM
dotnet xml:
Hi all,

Im looking for a better way to handle the exeption if you try to find a
node that doesnt exists?

It usualy breaks in a object exeption.

Now i have to use the try en catch option..

Any other ideas???

thanks in advance

Re: Looking for a better way to handle errors danprime
9/29/2005 10:12:32 AM

[quoted text, click to view]

I too came across this a lot. The strategy I adopted was to use a
foreach(XmlNode in NodeInQuestion.ChildNodes) and use a switch-case to
go through the possible nodenames-this will allow you to work with only
the nodes that you name, and easily refactored to allow for new ones.
Within the foreach you can set some kind of flag that you can later
check to see if a node existed or not.

Of course, if possible, you should always set up a schema and validate
through that but it may not be what you're looking for.

Hope that helps,
Dan'



--
danprime
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message1880477.html
Re: Looking for a better way to handle errors JJ
9/29/2005 1:27:23 PM
Martin Honnen <mahotrash@yahoo.de> wrote in
news:O1gq4zOxFHA.3720@TK2MSFTNGP11.phx.gbl:

[quoted text, click to view]

Hi,
You are wright..

I use the selectsinglenode("//path/something").innertext
When for some reason somthing doenst exists in the xmldoc it generates
an error.. In my case i would like it to result in nothing..

Re: Looking for a better way to handle errors Martin Honnen
9/29/2005 1:57:26 PM


[quoted text, click to view]


[quoted text, click to view]

C# pseudo code
XmlNode node = someNode.FirstChild;
if (node != null) {
// use node here
}

You have not told use how exactly you "try to find a node" so the above
is just one example of using the DOM to "try to find a node".

--

Martin Honnen --- MVP XML
Re: Looking for a better way to handle errors JJ
9/29/2005 2:36:08 PM
Hi martin,

That does the trick..

Thanks..

regards jj


Martin Honnen <mahotrash@yahoo.de> wrote in news:eMDuMqPxFHA.2072
@TK2MSFTNGP14.phx.gbl:

[quoted text, click to view]
Re: Looking for a better way to handle errors Martin Honnen
9/29/2005 3:34:38 PM


[quoted text, click to view]


[quoted text, click to view]

Well selectSingleNode returns a node or null (or Nothing in VB) so that
expression above is not good coding, do (VB pseudo code)
node = doc.SelectSingleNode("//path/something")
If Not node Is Nothing Then
' use node or node.InnerText here
End If
instead.


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button