473,785 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1745
Just construct a string as follows:

Dim myXml As String = "<?xml version="1.0" encoding="utf-8" ?>" &
ControlChars.Cr Lf _
& "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
& "<userdata> " & ControlChars.Cr Lf _
& ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
& ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
& ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
"</userid>" & ControlChars.Cr Lf _
& ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
& "</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.Cr Lf _
& "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
& "<userdata> " & ControlChars.Cr Lf _
& ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
& ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
& ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
"</userid>" & ControlChars.Cr Lf _
& ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
& "</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(fil eName)
Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
node.Attributes .Add(node.Creat eAttribute("nam e", 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.SelectSing leNode("./userid").Value = textBox2.Text
End If
xmlDoc.Save(fil eName)

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.Cr Lf _
& "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
& "<userdata> " & ControlChars.Cr Lf _
& ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
& ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
& ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
"</userid>" & ControlChars.Cr Lf _
& ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
& "</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(fil eName)
Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
node.Attributes .Add(node.Creat eAttribute("nam e", 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.SelectSing leNode("./userid").Value = textBox2.Text
End If
xmlDoc.Save(fil eName)

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.Cr Lf _
& "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
& "<userdata> " & ControlChars.Cr Lf _
& ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
& ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
& ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
"</userid>" & ControlChars.Cr Lf _
& ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
& "</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(fil eName)
Dim xPath As String = "//user[@name='" & userName & "''"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
' append attribute
Dim attr As XmlAttribute =
CType(xmlDoc.Cr eateNode(XmlNod eType.Attribute , "name", Nothing), XmlAttribute)
attr.Value = userName
node.Attributes .Append(attr)
' append userId node
Dim uidNode As XmlNode = xmlDoc.CreateEl ement("userid")
uidNode.AppendC hild(xmlDoc.Cre ateTextNode(use rId))
node.AppendChil d(uidNode)
' append user node
root.AppendChil d(node)
Else
Dim txtNode As XmlNode = node.SelectSing leNode("./userid").FirstC hild
If TypeOf txtNode Is XmlText Then
txtNode.Value = userId
End If
End If
xmlDoc.Save(fil eName)
Catch ex As Exception
System.Diagnost ics.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(fil eName)
Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
node.Attributes .Add(node.Creat eAttribute("nam e", 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.SelectSing leNode("./userid").Value = textBox2.Text
End If
xmlDoc.Save(fil eName)

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.Cr Lf _
> & "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
> & "<userdata> " & ControlChars.Cr Lf _
> & ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
> & ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
> & ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
> "</userid>" & ControlChars.Cr Lf _
> & ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
> & "</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(fil eName)
Dim xPath As String = "//user[@name='" & userName & "''"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
' append attribute
Dim attr As XmlAttribute =
CType(xmlDoc.Cr eateNode(XmlNod eType.Attribute , "name", Nothing), XmlAttribute)
attr.Value = userName
node.Attributes .Append(attr)
' append userId node
Dim uidNode As XmlNode = xmlDoc.CreateEl ement("userid")
uidNode.AppendC hild(xmlDoc.Cre ateTextNode(use rId))
node.AppendChil d(uidNode)
' append user node
root.AppendChil d(node)
Else
Dim txtNode As XmlNode = node.SelectSing leNode("./userid").FirstC hild
If TypeOf txtNode Is XmlText Then
txtNode.Value = userId
End If
End If
xmlDoc.Save(fil eName)
Catch ex As Exception
System.Diagnost ics.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(fil eName)
Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
node.Attributes .Add(node.Creat eAttribute("nam e", 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.SelectSing leNode("./userid").Value = textBox2.Text
End If
xmlDoc.Save(fil eName)

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.Cr Lf _
> > & "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
> > & "<userdata> " & ControlChars.Cr Lf _
> > & ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
> > & ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
> > & ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
> > "</userid>" & ControlChars.Cr Lf _
> > & ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
> > & "</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***@discussi ons.microsoft.c om> ¼¶¼g©ó¶l¥ó·s»D: C6************* *************** ******@microsof t.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(fil eName)
Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
node.Attributes .Add(node.Creat eAttribute("nam e", 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.SelectSing leNode("./userid").Value = textBox2.Text
End If
xmlDoc.Save(fil eName)

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.Cr Lf _
> > & "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
> > & "<userdata> " & ControlChars.Cr Lf _
> > & ControlChars.Ta b & "<user name=" & ControlChars.Qu ote &
> > textBox1.Text
> > & ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
> > & ControlChars.Ta b & ControlChars.Ta b & "<userid>" &
> > textBox2.Text &
> > "</userid>" & ControlChars.Cr Lf _
> > & ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
> > & "</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(fil eName)
Dim xPath As String = "//user[@name='" & userName & "''"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
' append attribute
Dim attr As XmlAttribute =
CType(xmlDoc.Cr eateNode(XmlNod eType.Attribute , "name", Nothing), XmlAttribute)
attr.Value = userName
node.Attributes .Append(attr)
' append userId node
Dim uidNode As XmlNode = xmlDoc.CreateEl ement("userid")
uidNode.AppendC hild(xmlDoc.Cre ateTextNode(use rId))
node.AppendChil d(uidNode)
' append user node
root.AppendChil d(node)
Else
Dim txtNode As XmlNode = node.SelectSing leNode("./userid").FirstC hild
If TypeOf txtNode Is XmlText Then
txtNode.Value = userId
End If
End If
xmlDoc.Save(fil eName)
Catch ex As Exception
System.Diagnost ics.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(fil eName)
Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
node.Attributes .Add(node.Creat eAttribute("nam e", 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.SelectSing leNode("./userid").Value = textBox2.Text
End If
xmlDoc.Save(fil eName)

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.Cr Lf _
> > & "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
> > & "<userdata> " & ControlChars.Cr Lf _
> > & ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
> > & ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
> > & ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
> > "</userid>" & ControlChars.Cr Lf _
> > & ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
> > & "</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(fil eName) 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(fil eName)
Dim xPath As String = "//user[@name='" & userName & "''"
Dim root As XmlNode = xmlDoc.Document Element
Dim node As XmlNode = root.SelectSing leNode(xPath)
If node Is Nothing Then
node = xmlDoc.CreateEl ement("user")
' append attribute
Dim attr As XmlAttribute =
CType(xmlDoc.Cr eateNode(XmlNod eType.Attribute , "name", Nothing), XmlAttribute)
attr.Value = userName
node.Attributes .Append(attr)
' append userId node
Dim uidNode As XmlNode = xmlDoc.CreateEl ement("userid")
uidNode.AppendC hild(xmlDoc.Cre ateTextNode(use rId))
node.AppendChil d(uidNode)
' append user node
root.AppendChil d(node)
Else
Dim txtNode As XmlNode = node.SelectSing leNode("./userid").FirstC hild
If TypeOf txtNode Is XmlText Then
txtNode.Value = userId
End If
End If
xmlDoc.Save(fil eName)
Catch ex As Exception
System.Diagnost ics.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(fil eName)
> Dim xPath As String = ""//user[@name='" & textBox1.Text & "']"
> Dim root As XmlNode = xmlDoc.Document Element
> Dim node As XmlNode = root.SelectSing leNode(xPath)
> If node Is Nothing Then
> node = xmlDoc.CreateEl ement("user")
> node.Attributes .Add(node.Creat eAttribute("nam e", 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.SelectSing leNode("./userid").Value = textBox2.Text
> End If
> xmlDoc.Save(fil eName)
>
> 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.Cr Lf _
> > > & "<!-- format is <user>userid</user> -->" & ControlChars.Cr Lf _
> > > & "<userdata> " & ControlChars.Cr Lf _
> > > & ControlChars.Ta b & "<user name=" & ControlChars.Qu ote & textBox1.Text
> > > & ControlChart.Qu ote & ">" & ControlChars.Cr Lf _
> > > & ControlChars.Ta b & ControlChars.Ta b & "<userid>" & textBox2.Text &
> > > "</userid>" & ControlChars.Cr Lf _
> > > & ControlChars.Ta b & "</user>" & ControlChars.Cr Lf _
> > > & "</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
16226
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? 1) simply UPDATING the values for all fields in the table, whether or not any particular field has actually changed 2) running a second SELECT statement and comparing the $_POST vars to the returned values, and only UPDATING those that have...
45
2684
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
5769
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 use of checkpoints (for simple recovery and Backup Log for full recovery). On several website people speak about full transaction log and the pace of growing can't keep up with the update. Therefore we want to create a script which flushes the...
1
2467
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... mem_id integer primary key lastname nvarchar(30) firstname nvarchar(30)
14
2962
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 utilise the disco file to update the Corresponding proxy file and reflect the changes made to the web service. However, the results of doing this with out params is that the results seem
6
4073
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 lost. So i have to provide some lock mechanism to that file in ftp, so how can i lock it, if one client opens it for updating, so that till it gets relased it cannot be updated by other clients. Could u please suggest me the simple solutions...
2
2859
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 200-300 Mb). After compacting the file shrinks back to 3-4 Mb. I have performed the following experiment. I created a test database containing only one table with two columns (Key number, Value text(50)). The table contains 10 thousands...
4
1992
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 allows the back-end file to be downloaded to the user's PC. This will provide maximum speed for these calculations/manipulations of data. Without this, just logging into the main app connected to the server back-end file can take five minutes...
3
1299
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 element or move element. The thing is, my script is a cmd based program (based on module "cmd"), and there are many users would use this script at same time, in a shell style prompt (module "cmd"), so I want to implement a
16
2983
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 something that PHP guys would know the answer to. I can't think of a more appropriate forum to try. I've heard the ASP and JSP guys aren't as friendly ;-) Let's say we have a HTML page from domain example.com, and that HTML page makes a request to...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.