Updating XML File

G

Guest

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>
 
G

Guest

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)...
 
G

Guest

how does it know what file to update?

Sergey Poberezovskiy said:
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)...
 
G

Guest

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
 
G

Guest

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.

Sergey Poberezovskiy said:
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


Rocky said:
how does it know what file to update?
 
G

Guest

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.

Sergey Poberezovskiy said:
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


Rocky said:
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>
 
G

Guest

I'll give it a try! Thank you sooo much!

Sergey Poberezovskiy said:
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.

Sergey Poberezovskiy said:
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>
 
L

Lau Lei Cheong

If you forgotten to import System.XML namespace, you'll have your VS.NET IDE
compile about it can't find the object you're talking about.

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.

Sergey Poberezovskiy said:
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


Rocky said:
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>
 
G

Guest

I tried this code, it compiles without any errors. I run it without any
errors, but it doesn't create a file called myfile.xml.

Sergey Poberezovskiy said:
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.

Sergey Poberezovskiy said:
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>
 
G

Guest

Rocky,

If you put a breakpoint to the line inside Catch block i wold say that you
should see an error similar to "File not found", and this error must be
generated by
xmlDoc.Load(fileName) line.

The code I used assumed that you already have a valid xml file, rather than
create one from within the application.

Rocky said:
I tried this code, it compiles without any errors. I run it without any
errors, but it doesn't create a file called myfile.xml.

Sergey Poberezovskiy said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top