Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with concatenating VB.NET code

Hello,

I am trying to concatenate "Receiver" with ".xml" in the code below, but the syntax is incorrect, can you please help me with the correct syntax.

 Dim xyz As String
        xyz = "Receiver"
            Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/+ " & xyz & "+ " & .xml & ""))

Thanks,

Victor
Avatar of djon2003
djon2003
Flag of Canada image

Well, this string is not correctly formed.
"~/App_Data/+ " & xyz & "+ " & .xml & ""

Are the + useful in the path ?
If yes :
"~/App_Data/+ " & xyz & "+ .xml "
If no :
"~/App_Data/" & xyz & ".xml "
Avatar of Victor  Charles

ASKER

Thanks for the code, would your code work for everywhere I want to replace "Receiver" with "xyz"? I have about 30 instances of the word Receiver.

 Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & ".xml & "))
        Dim LargestReceiverID = (From el In MyReceiver.Descendants("ReceiverTable") Select CInt(el.<Receiver_ID>.Value)).ToList
        LargestReceiverID.Sort()
        Dim CheckForItemReceiver = (From el In MyReceiver.Descendants("ReceiverTable").Elements("Receiver") Select el.Value).ToList
        If Not CheckForItemReceiver.Contains(Me.C1Receiver.Text.ToString) Then
            Dim xNew As XElement = New XElement("ReceiverTable")
            xNew.Add(New XElement("Receiver_ID", (LargestReceiverID.Last() + 1)))
            xNew.Add(New XElement("Receiver", Me.C1Receiver.Text.ToString))
            MyReceiver.Root.Add(xNew)
            MyReceiver.Save(Server.MapPath("~/App_Data/Receiver.xml"))
        End If
        Dim searchReceiver As String = C1Receiver.Text
        Dim searchIdReceiver As String = Nothing
        Dim domReceiver As New XmlDocument()
        Dim listReceiver As XmlNodeList = domReceiver.SelectNodes("//ReceiverTable")
        If CheckForItemReceiver.Contains(Me.C1Receiver.Text.ToString) Then
            For Each node As XmlNode In listReceiver
                If node("Receiver").InnerText.Equals(searchReceiver) Then
                    searchIdReceiver = node("Receiver_ID").InnerText
                    Exit For
                End If
            Next
        End If
I tried using the same approach, but getting lots of syntax errors.

Dim xyz As String
        xyz = "Receiver"
        ' Adding New Data
        '1-Add data to Receiver.xml if doesn't already exist
        Dim My& xyz &  As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & ".xml"))
        Dim Largest& xyz & ID = (From el In My& xyz & .Descendants("& xyz & Table") Select CInt(el.<& xyz & _ID>.Value)).ToList
        Largest&(xyz & ID.Sort())
        Dim CheckForItem& xyz &  = (From el In My& xyz & .Descendants("& xyz & Table").Elements("& xyz & ") Select el.Value).ToList
        If Not CheckForItem& xyz & .Contains(Me.C1& xyz & .Text.ToString) Then
            Dim xNew As XElement = New XElement("& xyz & Table")
            xNew.Add(New XElement("& xyz & _ID", (Largest& xyz & ID.Last() + 1)))
            xNew.Add(New XElement("& xyz & ", Me.C1& xyz & .Text.ToString))
            My&(xyz & .Root.Add(xNew))
            My&(xyz & .Save(Server.MapPath("~/App_Data/& xyz & .xml")))
        End If
        Dim search & xyz &  As String = C1& xyz & .Text
        Dim searchId& xyz &  As String = Nothing
        Dim dom& xyz &  As New XmlDocument()
        Dim list& xyz &  As XmlNodeList = dom& xyz & .SelectNodes("//& xyz & Table")
        If CheckForItem& xyz & .Contains(Me.C1& xyz & .Text.ToString) Then
            For Each node As XmlNode In list& xyz & 
                If node("& xyz & ").InnerText.Equals(search& xyz & ) Then
                    searchId& xyz &  = node("& xyz & _ID").InnerText
                    Exit For
                End If
            Next
        End If
Avatar of Nasir Razzaq
> Dim Largest& xyz & ID

You can not do that with declarations.
Do you have another approach?
>Dim search & xyz &  As String

Why do you need it that way? Can it not be

Dim search123 As String
Ehm.. You cannot apply a replace all from Receiver to & xyz &

Variable names cannot be dynamically create on the fly using concatenation. So set the variable names to something generic if the term receiver doesn't represent what you do in all the cases.

Let's take an example of modification (One the of the last line) :
searchIdReceiver = node("Receiver_ID").InnerText -->
searchId& xyz &  = node("& xyz & _ID").InnerText

Errors :
- Variable names don't accept spaces neither "&" char.
- This << "& xyz & _ID" >> is literally interpreted. So, the node as to have that name. The variable is not replaced because it is inside "xyz".

So the correction for that line should be (if we keep the variable name including the keyword intact) :
searchIdReceiver = node("Receiver_ID").InnerText -->
searchIdReceiver = node(xyz &"_ID").InnerText
I don't think I need to use the name of the xml file for all the variables, also I don't need
Dim searchId& xyz &  As String = Nothing and Dim searchId& xyz &  As String = Nothing


Can the following Dim  variables be fixed while they are being used with diffrenet xml files in For Next Loop?

 Dim MyFix  As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & ".xml"))
        Dim LargestFixID = (From el In My& xyz & .Descendants("& xyz & Table") Select CInt(el.<& xyz & _ID>.Value)).ToList
        LargestFixID.Sort()
        Dim CheckForItemFix = (From el In My& xyz & .Descendants("& xyz & Table").Elements("& xyz & ") Select el.Value).ToList


  Dim searchFix As String = C1& xyz & .Text
  Dim domFix As New XmlDocument()
  Dim listFix As XmlNodeList = domFix .SelectNodes("//& xyz & Table")
In think, from what I see, that you would greatly profit from reading some documents about the structure of VB.NET.

Here are two that can put you on the good track :
http://msdn.microsoft.com/en-us/library/7ee5a7s1(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/81ed9a62(v=vs.80).aspx
Thanks for the links, will get back to you.
Hi,

I read the links, but I still need help with the concatenating my statements. I reduced the number of xyz in my equation, but I neeed help with passing the values of xyz1...xyz3 to my statement.

Dim xyz1 As String
Dim xyz2 as String
Dim xyz3 as String
        xyz1 = "Receiver"
       xyz2 = "Donor"
       xyz3 = "Item"
        ' Adding New Data
        For x = 1 to 3
        Dim MyFix &  As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & xyz & & x &".xml"))
        Dim LargestFixID = (From el In My& xyz &  & x &".Descendants("& xyz &  & x &".Table") Select CInt(el.<& xyz &  & x &"_ID>.Value)).ToList
        LargestFixID.Sort())
        Dim CheckForItemFix  = (From el In My& xyz & & x &" .Descendants("& xyz &  & x &".Table").Elements("& xyz &  & x &".") Select el.Value).ToList
        If Not CheckForItemFix.Contains(Me.C1& xyz & & x &" .Text.ToString) Then
            Dim xNew As XElement = New XElement("& xyz &  & x &"Table")
            xNew.Add(New XElement("& xyz &  & x &" _ID", (LargestFixID.Last() + 1)))
            xNew.Add(New XElement("& xyz &  & x &", Me.C1& xyz &  & x &".Text.ToString))
            MyFix.Root.Add(xNew))
            MyFix .Save(Server.MapPath("~/App_Data/& xyz & & x &".xml")))
        End If
        Dim searchFix As String = C1& xyz & .Text
        Dim domFix  As New XmlDocument()
        Dim listFix As XmlNodeList = domFix.SelectNodes("//& xyz & & x &"Table")
        If CheckForItem& xyz & .Contains(Me.C1& xyz &  & x &".Text.ToString) Then
            For Each node As XmlNode In listFix
                If node("& xyz & & x &"").InnerText.Equals(search& xyz & & x &" ) Then
                    searchIdFix = node("& xyz & & x &" _ID").InnerText
                    Exit For
                End If
            Next
        End If
x = x+ 1
Next
ASKER CERTIFIED SOLUTION
Avatar of djon2003
djon2003
Flag of Canada image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank You very much for the tips and recommendations.