I'm in the process of converting all my applications from VB.Net to C#.
In the first version of this question I was striving for brevity but may have been too brief. So I'll try again.
In my limited experience, LINQ syntax in VB and C# is similar. For example, in VB I can write
Dim myArrayOfStateNames(3) As String
Sub FillStates()
myArrayOfStateNames(0) = "WA"
myArrayOfStateNames(1) = "OR"
myArrayOfStateNames(2) = "ID"
myArrayOfStateNames(3) = "CA"
End Sub
ReadOnly Property GetIdaho() As String
Get
Dim myState = From oneState In myArrayOfStateNames Where oneState = "ID" Select oneState
Return myState.First
End Get
End Property
Similarly in C# I can write
string[] myArrayOfStateNames = new string[4];
public void FillStates()
{
myArrayOfStateNames[0] = "WA";
myArrayOfStateNames[1] = "OR";
myArrayOfStateNames[2] = "ID";
myArrayOfStateNames[3] = "CA";
}
public string GetIdaho
{
get
{
var myState = from oneState in myArrayOfStateNames where oneState == "ID" select oneState;
return myState.First();
}
In these examples, the two LINQ statements after 'myState = ' are, to me anyway, very similar. The syntax is a little different, but close.
However, when I try to use LINQ with XML, things are apparently not so straightforward. In VB I can write
Dim myXML As New System.Xml.Linq.XDocument
Sub MakeSomeXML()
myXML = _
="1.0"="utf-8"
="7.0"
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<ItemQueryRq>
<FullName>Joe Jones</FullName>
<OwnerID>0</OwnerID>
</ItemQueryRq>
</QBXMLMsgsRq>
</QBXML>
End Sub
ReadOnly Property XMLName() As String
Get
Dim name = From someXML In myXML...<ItemQueryRq> Select someXML.<FullName>
Return name.First.Value
End Get
End Property
But when I try to convert to C# like this
System.Xml.Linq.XDocument myXML;
public void MakeSomeXML()
{
System.Xml.Linq.XDocument myXML = System.Xml.Linq.XDocument.Parse(
@" =""=""
=""
<QBXML>
<QBXMLMsgsRq onError=""stopOnError"">
<ItemQueryRq>
<FullName>Joe Jones</FullName>
<OwnerID>0</OwnerID>
</ItemQueryRq>
</QBXMLMsgsRq>
</QBXML>");
}
public string XMLName
{
get
{
string name = from someXML in myXML...<ItemQueryRq> select someXML.<FullName>;
return name;
}
}
C# chokes badly on the linq syntax after 'from someXML in myXML' and the intellisense in C# does not show '...<>' as it does in VB. the syntax is seemingly not at all similar.
So two questions:
1) am I missing something obvious?
2) if not, what is the proper/best way to approach this in C#? The LINQ syntax in the C# example is obviously wrong, is it correctable or am I on a totally wrong path? Should I use a 'from someXML in myXML' type of statement buth with a different syntax? Use XMLReader instead? Use XPath?
Thanks much.