Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : XmlElementAttribute: How to check if it exists.


Thomas Wolf
2/28/2005 12:23:04 AM
If the WSDL of a web service defines a complextype with the attribute X how
do I examine if X is returned after the call?

Definition:



[System.Xml.Serialization.XmlElementAttribute("X")]



public string[] X { get; set; }




The problem is that I get a NullReference Exception if X is not part of the
returned XML.

The following C# code doesn't work:



if (obj.objAttributes.X[0] == null)

//do something

But in VB.Net for example the code

If (Not(obj.objAttributes.X(0) Is Nothing) Then

'do something

End If

seems to work.

What ist the difference?

How to check if obj.objAttributes.X[0] exists with C#?

Thank you.

Thomas
--
Thomas Wolf M. A.

http://www25.brinkster.com/WebSolutions1/
Dinh Duy Tran
2/28/2005 8:23:14 AM
Thomas,

In C#, you can uses IList.Contains() to checl if an attribute is exist:

if ( !obj.Attributes.Contains("X") )
{
...
}

Dinhduy
CSDP/MCSD

[quoted text, click to view]

Dennis Myrén
2/28/2005 2:23:39 PM
Guess it is a typo, but should'nt
if (obj.objAttributes.X[0] == null)
be
if (obj.objAttributes.X[0] != null)

anyway
If (Not(obj.objAttributes.X(0) Is Nothing) Then
=
if (obj.objAttributes.X[0] != null)

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
[quoted text, click to view]

AddThis Social Bookmark Button