Reputation: 734
I have a project to read xml files. It comes from a web service. But it can come sometimes with different attributes.
The standart xml file is like this:
<xml ....>
<car carname="Opel">Opel
</car>
</xml>
However sometimes web service can send me a xml file like this:
<xml ....>
<car carName="Opel">Opel
</car>
</xml>
In code behind;
If carrierNode.Attributes("carname") IsNot Nothing Then
CarrierLabel = carrierNode.Attributes("carname").Value
...............
Now, when second type XML file came to my code, it blow out because of there is not such an attribute with "carName". How can I support variations of carName like "CarName", "carNamE" "CARNAME" .... ?
Thanks
Upvotes: 0
Views: 3144
Reputation: 6956
You do not need to support all case variations. XML is case sensitive, meaning that carName
and carname
are two different attributes. You can't safely assume that just because two attributes have similar names, they are the same.
If the service conforms to a published schema or DTD, then you can look in there to find out which attributes an element may have.
If there is no published schema, then ask the provider if they have one.
If no schema is used at all, then it is likely that this behaviour is not intentional, and that you should file a bug with the web service provider.
When carname
is expected, providing carName
is the same kind of error as providing carmame
, caarname
, canrame
, car_name
or car-name
. A service could not reasonably be expected to respond correctly given all possible variations of erroneous input. The variations are extremely numerous. The appropriate course of action, when given erroneous input is to respond with an informative error message, allowing the other end to correct their mistake.
If you explicitly wish to support certain known variations such as carname
and carName
, then do so for those variations that you feel it is reasonable to expect, and provide appropriate and informative error messages for anything else.
Upvotes: 1
Reputation: 17792
Assuming you are using Visual Basic .Net and XLinq:
I'm not sure this is correct Visual Basic .Net - you might have to fix it a few places.
Dim attribute = (From a In carrierNode.Attributes() _
Where a.Name.ToLower() = "carname" _
Select a).FirstOrDefault()
If attribute IsNot Nothing Then
CarrierLabel = attribute.Value
...
Upvotes: 1