Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
VB
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
C#
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
XML
Dim myXML As New System.Xml.Linq.XDocument
 Sub MakeSomeXML()
     myXML = _
  <?xml version="1.0" encoding="utf-8"?>
  <?qbxml version="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
XML
System.Xml.Linq.XDocument myXML;
        
        public void MakeSomeXML()
        {
            System.Xml.Linq.XDocument myXML = System.Xml.Linq.XDocument.Parse(
            @" <?xml version=""1.0"" encoding=""utf-8""?>
                <?qbxml version=""7.0""?>
                    <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.
Posted
Updated 10-Feb-10 9:11am
v3

You have onle posted the original VB code, perhaps you could add the C# code that you have written and then someone may be able to help you.
 
Share this answer
 
In response to the one answer (and thanks for the answer) I went for verbosity and rewrote the question.
 
Share this answer
 
Dell.Simmons wrote:
string name = from someXML in myXML...<itemqueryrq> select someXML.<fullname>;


I don't think that this is C#, the lack of intellisense is usually a clue. However, I have no idea what it should be as I'm not that familiar with VB.
 
Share this answer
 
Well, I still don't know the proper/best way to do this, but I found a way that works:
C#
public string XMLName
       {
           get
           {
             var name = from someXML in myXML.Descendants()
                         where someXML.Name == "FullName"
                         select someXML.Value;

             return name.First<string>();
           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900