473,804 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ 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 17813
cloudx <cl****@discuss ions.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.co m>
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****@discuss ions.microsoft. com> wrote in message
news:68******** *************** ***********@mic rosoft.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****@discuss ions.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.co m>
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 (XmlFormatExcep tion e)
{
// it wasn't XML if we get here, so do the other thing
}
finally
{
CloseXML();
}

/m
"cloudx" <cl****@discuss ions.microsoft. com> wrote in message
news:01******** *************** ***********@mic rosoft.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****@discuss ions.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.co m>
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
2635
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 feasible. It would be helpful if people could test the patched Python with their own applications and report any incompatibilities. PEP: 349
4
6910
by: Alex Vinokur | last post by:
I have got two functions: void foo1(char* str) { // Stuff } void foo2(int size) { char* str;
8
2215
by: A.M | last post by:
Hi, Are string parameters in functions by refrence or by value ? Thanks, Ali
2
36876
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 val = { 1.0, 2.0, 3.0, 4.0 }; string fmt = "{0} {1} {2} {3}"; The following variations of string.Format() will work just fine with strings: string s1 = String.Format(fmt, str, str, str, str); // works
23
2501
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 bits no more than 32 bits long: ");
10
6192
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 : Any idea why it fails? char * C_recReverse(char * str)
3
2497
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") { DOMParser = function () {} DOMParser.prototype.parseFromString = function (str, contentType) { if (typeof ActiveXObject != "undefined") { var d =...
16
3037
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 warning option to Python. Start Python as "python -bb" and try it again. ;) Christian
1
2777
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). The first function is called "StrFirst" and takes in a LPTSTR as the first parameter and a long as the second, like this: SIMPLELIB_API int StrFirst(LPTSTR str, long num); The second function is essentially the same thing, but with the order of the...
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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 we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.