Victor Charles
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.MapP ath("~/App _Data/+ " & xyz & "+ " & .xml & ""))
Thanks,
Victor
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.MapP
Thanks,
Victor
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.MapP ath("~/App _Data/" & xyz & ".xml & "))
Dim LargestReceiverID = (From el In MyReceiver.Descendants("Re ceiverTabl e") Select CInt(el.<Receiver_ID>.Valu e)).ToList
LargestReceiverID.Sort()
Dim CheckForItemReceiver = (From el In MyReceiver.Descendants("Re ceiverTabl e").Elemen ts("Receiv er") Select el.Value).ToList
If Not CheckForItemReceiver.Conta ins(Me.C1R eceiver.Te xt.ToStrin g) 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.ToStrin g))
MyReceiver.Root.Add(xNew)
MyReceiver.Save(Server.Map Path("~/Ap p_Data/Rec eiver.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("/ /ReceiverT able")
If CheckForItemReceiver.Conta ins(Me.C1R eceiver.Te xt.ToStrin g) Then
For Each node As XmlNode In listReceiver
If node("Receiver").InnerText .Equals(se archReceiv er) Then
searchIdReceiver = node("Receiver_ID").InnerT ext
Exit For
End If
Next
End If
Dim MyReceiver As XDocument = XDocument.Load(Server.MapP
Dim LargestReceiverID = (From el In MyReceiver.Descendants("Re
LargestReceiverID.Sort()
Dim CheckForItemReceiver = (From el In MyReceiver.Descendants("Re
If Not CheckForItemReceiver.Conta
Dim xNew As XElement = New XElement("ReceiverTable")
xNew.Add(New XElement("Receiver_ID", (LargestReceiverID.Last() + 1)))
xNew.Add(New XElement("Receiver", Me.C1Receiver.Text.ToStrin
MyReceiver.Root.Add(xNew)
MyReceiver.Save(Server.Map
End If
Dim searchReceiver As String = C1Receiver.Text
Dim searchIdReceiver As String = Nothing
Dim domReceiver As New XmlDocument()
Dim listReceiver As XmlNodeList = domReceiver.SelectNodes("/
If CheckForItemReceiver.Conta
For Each node As XmlNode In listReceiver
If node("Receiver").InnerText
searchIdReceiver = node("Receiver_ID").InnerT
Exit For
End If
Next
End If
ASKER
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.MapP ath("~/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("~/Ap p_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
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.MapP
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("~/Ap
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
searchId& xyz & = node("& xyz & _ID").InnerText
Exit For
End If
Next
End If
> Dim Largest& xyz & ID
You can not do that with declarations.
You can not do that with declarations.
ASKER
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
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").InnerT ext -->
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").InnerT ext -->
searchIdReceiver = node(xyz &"_ID").InnerText
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").InnerT
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").InnerT
searchIdReceiver = node(xyz &"_ID").InnerText
ASKER
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.MapP ath("~/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")
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.MapP
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
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
ASKER
Thanks for the links, will get back to you.
ASKER
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.MapP ath("~/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(M e.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("~/Ap p_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(sear ch& xyz & & x &" ) Then
searchIdFix = node("& xyz & & x &" _ID").InnerText
Exit For
End If
Next
End If
x = x+ 1
Next
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.MapP
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(M
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("~/Ap
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(sear
searchIdFix = node("& xyz & & x &" _ID").InnerText
Exit For
End If
Next
End If
x = x+ 1
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You very much for the tips and recommendations.
"~/App_Data/+ " & xyz & "+ " & .xml & ""
Are the + useful in the path ?
If yes :
"~/App_Data/+ " & xyz & "+ .xml "
If no :
"~/App_Data/" & xyz & ".xml "