473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Updating XML File

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>
Dec 28 '05 #1
9 1733
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)...

"Rocky" wrote:
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>

Dec 29 '05 #2
how does it know what file to update?

"Sergey Poberezovskiy" wrote:
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)...

"Rocky" wrote:
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>

Dec 29 '05 #3
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" wrote:
how does it know what file to update?

"Sergey Poberezovskiy" wrote:
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)...

"Rocky" wrote:
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>

Dec 29 '05 #4
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" wrote:
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" wrote:
how does it know what file to update?

"Sergey Poberezovskiy" wrote:
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)...

"Rocky" wrote:

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

Dec 29 '05 #5
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" wrote:
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" wrote:
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" wrote:
how does it know what file to update?

"Sergey Poberezovskiy" wrote:

> 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)...
>
> "Rocky" wrote:
>
> > 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>

Dec 29 '05 #6
I'll give it a try! Thank you sooo much!

"Sergey Poberezovskiy" wrote:
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" wrote:
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" wrote:
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" wrote:

> how does it know what file to update?
>
> "Sergey Poberezovskiy" wrote:
>
> > 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)...
> >
> > "Rocky" wrote:
> >
> > > 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>

Dec 29 '05 #7
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" <Ro***@discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D:C6********************************* *@microsoft.com...
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" wrote:
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" wrote:
> how does it know what file to update?
>
> "Sergey Poberezovskiy" wrote:
>
> > 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)...
> >
> > "Rocky" wrote:
> >
> > > 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>

Dec 30 '05 #8
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" wrote:
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" wrote:
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" wrote:
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" wrote:

> how does it know what file to update?
>
> "Sergey Poberezovskiy" wrote:
>
> > 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)...
> >
> > "Rocky" wrote:
> >
> > > 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>

Dec 30 '05 #9
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" wrote:
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" wrote:
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" wrote:
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" wrote:

> 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" wrote:
>
> > how does it know what file to update?
> >
> > "Sergey Poberezovskiy" wrote:
> >
> > > 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)...
> > >
> > > "Rocky" wrote:
> > >
> > > > 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>

Jan 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
6
by: Hennie de Nooijer | last post by:
Hi, Currently we're a building a metadatadriven datawarehouse in SQL Server 2000. We're investigating the possibility of the updating tables with enormeous number of updates and insert and the...
1
by: Luis Esteban Valencia | last post by:
Hello Everyone, Iam an intermediate ASP.Net programmer and iam facing a challenging task. I have a table in MS-SQL server database called 'Members'. The table has following fields... ...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
6
by: muttu2244 | last post by:
hi all am updating the same file in ftp, through multiple clients, but am scared that two clients may open the same file at a time, and try updating, then the data updated by one data will be...
2
by: Alexey.Murin | last post by:
The application we are developing uses MS Access 2003 database (with help of ADO). We have noticed that during massive records updating the size of the mdb file increases dramatically (from 3-4 to...
4
by: rdemyan via AccessMonster.com | last post by:
My application is calculation intensive and the servers are agonizingly slow. Administrators of my application only update the backends once a month (twice a month max). So, my launching program...
3
by: Evan | last post by:
Hello - My script use a DB file which is written by XML, and the user load this DB file (XML tree in memory), and then do some updating about this tree, such as delete element, generate new...
16
by: Stevo | last post by:
I'm guessing this is a laughably obvious answer to many here, but it's not to me (and I don't have a server or any knowledge of PHP to be able to try it). It's not strictly a PHP question, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.