Answered by:
Linq to XML (vb to c#) question

Question
-
Hi, I am a beginning C# programmer and rely a lot on code conversion routines to help me migrate my vb code. Linq, however, is not supported. Can someone get me started with how this would conver to C#
Thanks,
Fred
Dim dom As XDocument = XDocument.Load(ofd.FileName)
Dim FieldStructure = From bx In dom...<FieldMD>
For Each bx In FieldStructure
MyFieldNodes(RecordCounter).StructureID = CInt(bx.<StructureId>.Value)
MyFieldNodes(RecordCounter).SequenceNumber = CInt(bx.<SequenceNumber>.Value)
MyFieldNodes(RecordCounter).Name = (bx.<Name>.Value)
MyFieldNodes(RecordCounter).ShortenedName = (bx.<ShortenedName>.Value)Wednesday, July 1, 2009 11:03 PM
Answers
-
I couldn't validate the syntax with a compile because there is too much here I don't have.
But this should be close:
XDocument dom = XDocument.Load(ofd.FileName); var FieldStructure = dom.Descendants("FieldMD"); foreach (var bx in FieldStructure) { MyFieldNodes[RecordCounter].StructureID = Convert.ToInt32(bx.Element("StructureId").Value); MyFieldNodes[RecordCounter].SequenceNumber = Convert.ToInt32(bx.Element("SequenceNumber").Value); MyFieldNodes[RecordCounter].Name = bx.Element("Name").Value; MyFieldNodes[RecordCounter].ShortenedName = bx.Element("ShortenedName").Value; }
Hope this helps.
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- Marked as answer by Vitek Karas - MSFTMicrosoft employee, Moderator Friday, July 3, 2009 8:25 AM
Wednesday, July 1, 2009 11:23 PM -
Here is some translation, not a one to one but rather an attempt to make use of LINQ to XML features:
XDocument doc = XDocument.Load(ofd.FileName); foreach (XElement field in doc.Descendants("FieldMD")) { MyFieldNodes[RecordCounter].StructureID = (int)field.Element("StructureID"), MyFieldNodes[RecordCounter].SequenceNumber = (int)field.Element("SequenceNumber"), MyFieldNodes[RecordCounter].Name = (string)field.Element("Name"), MyFieldNodes[RecordCounter].ShortenedName = (string)field.Element("ShortenedName") }
So the main deviation from the VB code is to make use of all the explicit conversion operations that XElement defines to e.g. directly cast an XElement to an int or string.
MVP XML My blog- Proposed as answer by Pawel KadluczkaModerator Thursday, July 2, 2009 5:03 PM
- Marked as answer by Vitek Karas - MSFTMicrosoft employee, Moderator Friday, July 3, 2009 8:24 AM
Thursday, July 2, 2009 11:28 AM
All replies
-
Bummer that you have to move this to C# because it does not support XML literals. So you can't do any of this: <StructureId> type of thing.
You can mix VB and c# projects in a solution. I do it all the time. Don't know if this is an option for you.
In the mean time ... I am working on this ...
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!Wednesday, July 1, 2009 11:17 PM -
I couldn't validate the syntax with a compile because there is too much here I don't have.
But this should be close:
XDocument dom = XDocument.Load(ofd.FileName); var FieldStructure = dom.Descendants("FieldMD"); foreach (var bx in FieldStructure) { MyFieldNodes[RecordCounter].StructureID = Convert.ToInt32(bx.Element("StructureId").Value); MyFieldNodes[RecordCounter].SequenceNumber = Convert.ToInt32(bx.Element("SequenceNumber").Value); MyFieldNodes[RecordCounter].Name = bx.Element("Name").Value; MyFieldNodes[RecordCounter].ShortenedName = bx.Element("ShortenedName").Value; }
Hope this helps.
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- Marked as answer by Vitek Karas - MSFTMicrosoft employee, Moderator Friday, July 3, 2009 8:25 AM
Wednesday, July 1, 2009 11:23 PM -
Clarify what you mean by "Linq, however, is not supported".
C# 2008 supports XDocument and LINQ, but it does not support the VB.NET navigation features that you are using.
Are you willing to keep using XDocument, or do you want to get rid of that too? The main reason would be if you are porting to C# 2005 instead of 2008.Wednesday, July 1, 2009 11:24 PM -
I think that the OP meant that the majority of the free code converters don't support converting Linq. That is how I read it anyway.
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!Wednesday, July 1, 2009 11:28 PM -
Here is some translation, not a one to one but rather an attempt to make use of LINQ to XML features:
XDocument doc = XDocument.Load(ofd.FileName); foreach (XElement field in doc.Descendants("FieldMD")) { MyFieldNodes[RecordCounter].StructureID = (int)field.Element("StructureID"), MyFieldNodes[RecordCounter].SequenceNumber = (int)field.Element("SequenceNumber"), MyFieldNodes[RecordCounter].Name = (string)field.Element("Name"), MyFieldNodes[RecordCounter].ShortenedName = (string)field.Element("ShortenedName") }
So the main deviation from the VB code is to make use of all the explicit conversion operations that XElement defines to e.g. directly cast an XElement to an int or string.
MVP XML My blog- Proposed as answer by Pawel KadluczkaModerator Thursday, July 2, 2009 5:03 PM
- Marked as answer by Vitek Karas - MSFTMicrosoft employee, Moderator Friday, July 3, 2009 8:24 AM
Thursday, July 2, 2009 11:28 AM