The following code should compile just fine (provided that you have textBox1
and textBox2 TextBoxes on your form):
Try
Dim fileName As String = "myFile.xml"
Dim userName As String = textBox1.Text
Dim userId As String = textBox2.Text
Dim xmlDoc As XmlDocument = New XmlDocument
xmlDoc.Load(fileName)
Dim xPath As String = "//user[@name='" & userName & "''"
Dim root As XmlNode = xmlDoc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateElement("user")
' append attribute
Dim attr As XmlAttribute =
CType(xmlDoc.CreateNode(XmlNodeType.Attribute, "name", Nothing), XmlAttribute)
attr.Value = userName
node.Attributes.Append(attr)
' append userId node
Dim uidNode As XmlNode = xmlDoc.CreateElement("userid")
uidNode.AppendChild(xmlDoc.CreateTextNode(userId))
node.AppendChild(uidNode)
' append user node
root.AppendChild(node)
Else
Dim txtNode As XmlNode = node.SelectSingleNode("./userid").FirstChild
If TypeOf txtNode Is XmlText Then
txtNode.Value = userId
End If
End If
xmlDoc.Save(fileName)
Catch ex As Exception
System.Diagnostics.Debug.Write(ex.ToString)
End Try
HTH
Rocky said:
When I put this code into visual studio, i'm getting a lot of errors
underlined in blue. Can you try it, then you'll see what i'm talking about.
:
Not sure what are your rules to pick a file, but if you need to update an
existing xml file you could use something similar to the following:
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(fileName)
Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
Dim root As XmlNode = xmlDoc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateElement("user")
node.Attributes.Add(node.CreateAttribute("name", textBox1.Text))
' add userid node to user here
...
' now add created node to the document
root.ChildNodes.Add(node)
Else
' update the existing node
node.SelectSingleNode("./userid").Value = textBox2.Text
End If
xmlDoc.Save(fileName)
I am not sure about the syntax, as I do not have VS in front of me - but you
can check the help if something does not compile...
And do not forget to wrap the whole thing into Try..Catch block
:
how does it know what file to update?
:
Just construct a string as follows:
Dim myXml As String = "<?xml version="1.0" encoding="utf-8" ?>" &
ControlChars.CrLf _
& "<!-- format is <user>userid</user> -->" & ControlChars.CrLf _
& "<userdata>" & ControlChars.CrLf _
& ControlChars.Tab & "<user name=" & ControlChars.Quote & textBox1.Text
& ControlChart.Quote & ">" & ControlChars.CrLf _
& ControlChars.Tab & ControlChars.Tab & "<userid>" & textBox2.Text &
"</userid>" & ControlChars.CrLf _
& ControlChars.Tab & "</user>" & ControlChars.CrLf _
& "</userdata>"
Or you could omit the tabs and carriage returns as they are ingnored by xml
parser anyhow.
Alternatively, you could create an XML document and use .Net XML library to
add nodes into it and then save the result into a file (string or stream)...
:
I have 2 textboxes. When I click submit, i want to add whatevers in the text
box1 as username and whatevers in textbox2 as userid into an xml file. How do
I do that in ASP.NET using vb.net?
My xml file look like this:
<?xml version="1.0" encoding="utf-8" ?>
<!-- format is <user>userid</user> -->
<userdata>
<user name="Username1">
<userid>abc1</userid>
</user>
</userdata>