Updating XML File

Discussion in 'ASP .Net' started by =?Utf-8?B?Um9ja3k=?=, Dec 28, 2005.

  1. 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>
     
    =?Utf-8?B?Um9ja3k=?=, Dec 28, 2005
    #1
    1. Advertising

  2. 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>
     
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Dec 29, 2005
    #2
    1. Advertising

  3. 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>
     
    =?Utf-8?B?Um9ja3k=?=, Dec 29, 2005
    #3
  4. 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>
     
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Dec 29, 2005
    #4
  5. 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>
     
    =?Utf-8?B?Um9ja3k=?=, Dec 29, 2005
    #5
  6. 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>
     
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Dec 29, 2005
    #6
  7. 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>
     
    =?Utf-8?B?Um9ja3k=?=, Dec 29, 2005
    #7
  8. 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" <> ¼¶¼g©ó¶l¥ó·s»D:...
    > 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>
     
    Lau Lei Cheong, Dec 30, 2005
    #8
  9. 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>
     
    =?Utf-8?B?Um9ja3k=?=, Dec 30, 2005
    #9
  10. 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>
     
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=, Jan 3, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9lbA==?=

    Updating an XML file from a URL

    =?Utf-8?B?Sm9lbA==?=, Oct 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    607
    Kevin Spencer
    Oct 21, 2005
  2. David Nettles
    Replies:
    0
    Views:
    430
    David Nettles
    May 8, 2004
  3. Replies:
    1
    Views:
    5,957
    William Brogden
    Aug 22, 2006
  4. news.rcn.com
    Replies:
    2
    Views:
    1,180
    Roedy Green
    Dec 10, 2007
  5. Erik Wasser
    Replies:
    5
    Views:
    520
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page