I am trying to check whether or not a string is exists in an xml file. If it does not exist, I have to add that string to the xml file.
My xml file looks like this:
<Employee>
<Emp>
<Name id="1"> A </Name>
<Name id="2"> C </Name>
<Name id="3"> D </Name>
</Emp>
</Employee>
And I want achieve:
dim str as string = "b"
if str exists in "xmlfile"
'do something
else
'add string to file
end if
I am not really familiar LINQ at all but I understand that what I want to do is possible with LINQ. I have tried the following:
Dim employee = XElement.Load(someStream)
Dim emp = employee.Element("Emp")
If emp IsNot Nothing Then
else
'add string to xml file
End If
When I tried this I received the error: XElement is not declared.
Can any one suggest a way to do what I want in VB.NET without LINQ? Or if I have to use LINQ, can some provide an example?
I am using .net 2.0 (VB.NET)
What I think you want is something more like this:
So that’s an example using XDocument and interating though like user1512185 suggested with an example LINQ expression to get a list of XElements to iterate through.
This is a modification of something I do in one of my apps. However I don’t insert into the xml so I’m not sure I can help you there.