473,473 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to check if a string is an XML before calling LoadXML(str)?

hi there, in VB LoadXML(str) returns true or false so that you can do
different coding when the str is XML or not, but in C# LoadXML doesn't have
return but only throw exception if str is not XML document. How would I do if
I want to do my own code if str is not an XML? thanks!
Nov 17 '05 #1
4 17790
cloudx <cl****@discussions.microsoft.com> wrote:
hi there, in VB LoadXML(str) returns true or false so that you can do
different coding when the str is XML or not, but in C# LoadXML doesn't have
return but only throw exception if str is not XML document. How would I do if
I want to do my own code if str is not an XML? thanks!


Anything wrong with catching the exception? It seems the simplest way
to me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
If you start to parse the whole infoset to see if it is all valid xml, then
you may as well LoadXML as that what it does anyway. If you want a quick
check/guess, test to see if first char of string is "<". If not, you know
it is not xml. You don't know it is valid xml either, but the load will
tell you that. You may want to Trim() the string first just to remove any
leading or trailing spaces and test for Empty first, then for leading "<"
char.

--
William Stacey [MVP]

"cloudx" <cl****@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
hi there, in VB LoadXML(str) returns true or false so that you can do
different coding when the str is XML or not, but in C# LoadXML doesn't
have
return but only throw exception if str is not XML document. How would I do
if
I want to do my own code if str is not an XML? thanks!

Nov 17 '05 #3
I am new to C# so I might be asking some silly questions. The issue is that
if the str is not XML then I would like to do something else instead of being
kicked out by exception. Thanks!

"Jon Skeet [C# MVP]" wrote:
cloudx <cl****@discussions.microsoft.com> wrote:
hi there, in VB LoadXML(str) returns true or false so that you can do
different coding when the str is XML or not, but in C# LoadXML doesn't have
return but only throw exception if str is not XML document. How would I do if
I want to do my own code if str is not an XML? thanks!


Anything wrong with catching the exception? It seems the simplest way
to me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4
Catching an Exception is not necessarily being "kicked out." While it's not
the most efficient way to handle an error, it certainly wouldn't be that
drastic. For example:

try
{
LoadXML(str);
... // do something with the XML
}
catch (XmlFormatException e)
{
// it wasn't XML if we get here, so do the other thing
}
finally
{
CloseXML();
}

/m
"cloudx" <cl****@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
I am new to C# so I might be asking some silly questions. The issue is that
if the str is not XML then I would like to do something else instead of
being
kicked out by exception. Thanks!

"Jon Skeet [C# MVP]" wrote:
cloudx <cl****@discussions.microsoft.com> wrote:
> hi there, in VB LoadXML(str) returns true or false so that you can do
> different coding when the str is XML or not, but in C# LoadXML doesn't
> have
> return but only throw exception if str is not XML document. How would I
> do if
> I want to do my own code if str is not an XML? thanks!


Anything wrong with catching the exception? It seems the simplest way
to me.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

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

Similar topics

2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
4
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
8
by: A.M | last post by:
Hi, Are string parameters in functions by refrence or by value ? Thanks, Ali
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
23
by: mrvendetta1 | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 33 void StringToIntArray(const char *s1); void takeinteger(int i); int main() { char string; printf("enter a string of...
10
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
3
gagandeepgupta16
by: gagandeepgupta16 | last post by:
I am having problem in parsing a string containing HTML Tags. The situation is somewhat similar to the following as quoted in some other forum : if (typeof DOMParser == "undefined") { ...
16
by: Christian Heimes | last post by:
Gabriel Genellina schrieb: Yes, it's on purpose but it's a bug in your application to call str() on a bytes object or to compare bytes and unicode directly. Several months ago I added a bytes...
1
by: JohnCox | last post by:
I have a simple Win32 DLL I wrote named "SimpleLib" that exports two functions. It is written in C++ and compiled with __stdcall (/Gz) and with the preprocessor definition _MBCS (not Unicode). ...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.