I am having problems with working with a third party XML string that contains a Namespace with LINQ to XML.
In the below code everything works find. I am able to select the xElement (xEl1) and update its value.
'Example Without Namespace
Dim XmlWithOutNs = _
<?xml version="1.0"?>
<RATABASECALC>
<RATEREQUEST>
<ANCHOR>
<DATABASENAME>DatabaseName</DATABASENAME>
<DATABASEPW>DatabasePw</DATABASEPW>
</ANCHOR>
</RATEREQUEST>
</RATABASECALC>
Dim xEl1 = XmlWithOutNs...<DATABASEPW>.FirstOrDefault
If xEl1 IsNot Nothing Then
xEl1.Value = "test"
End If
However, in the below code the xElement (xEl2) returns Nothing. The only difference is the Namespace (xmlns=”http://www.cgi.com/Ratabase)
'Example With Namespace
Dim XmlWithNs = _
<?xml version="1.0"?>
<RATABASECALC xmlns="http://www.cgi.com/Ratabase">
<RATEREQUEST>
<ANCHOR>
<DATABASENAME>DatabaseName</DATABASENAME>
<DATABASEPW>DatabasePw</DATABASEPW>
</ANCHOR>
</RATEREQUEST>
</RATABASECALC>
Dim xEl2 = XmlWithNs...<DATABASEPW>.FirstOrDefault
If xEl2 IsNot Nothing Then
xEl2.Value = "test"
End If
So my questions are:
1. Why is this happening?
2. How do I resolve this issue?
Doesn’t that compile to the equivalent of (in C# terms):
where-as to get “DATABASEPW” in the right namespace you would need the equivalent of:
Translate to VB and you should be set?
Reflector assures me (but don’t quote me!) that this is something like: