Updating XML File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rocky

    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>
  • Sergey Poberezovskiy

    #2
    RE: Updating XML File

    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:
    [color=blue]
    > 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>[/color]

    Comment

    • Rocky

      #3
      RE: Updating XML File

      how does it know what file to update?

      "Sergey Poberezovskiy" wrote:
      [color=blue]
      > 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:
      >[color=green]
      > > 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>[/color][/color]

      Comment

      • Sergey Poberezovskiy

        #4
        RE: Updating XML File

        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:
        [color=blue]
        > how does it know what file to update?
        >
        > "Sergey Poberezovskiy" wrote:
        >[color=green]
        > > 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:
        > >[color=darkred]
        > > > 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>[/color][/color][/color]

        Comment

        • Rocky

          #5
          RE: Updating XML File

          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:
          [color=blue]
          > 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:
          >[color=green]
          > > how does it know what file to update?
          > >
          > > "Sergey Poberezovskiy" wrote:
          > >[color=darkred]
          > > > 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>[/color][/color][/color]

          Comment

          • Sergey Poberezovskiy

            #6
            RE: Updating XML File

            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:
            [color=blue]
            > 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:
            >[color=green]
            > > 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:
            > >[color=darkred]
            > > > 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>[/color][/color][/color]

            Comment

            • Rocky

              #7
              RE: Updating XML File

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

              "Sergey Poberezovskiy" wrote:
              [color=blue]
              > 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:
              >[color=green]
              > > 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:
              > >[color=darkred]
              > > > 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>[/color][/color][/color]

              Comment

              • Lau Lei Cheong

                #8
                Re: Updating XML File

                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" <Rocky@discussi ons.microsoft.c om> ¼¶¼g©ó¶l¥ó·s»D: C60BF055-92B4-4068-87EC-2C6C9956411F@mi crosoft.com...[color=blue]
                > 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:
                >[color=green]
                >> 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:
                >>[color=darkred]
                >> > 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>[/color][/color][/color]


                Comment

                • Rocky

                  #9
                  RE: Updating XML File

                  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:
                  [color=blue]
                  > 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:
                  >[color=green]
                  > > 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:
                  > >[color=darkred]
                  > > > 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>[/color][/color][/color]

                  Comment

                  • Sergey Poberezovskiy

                    #10
                    RE: Updating XML File

                    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:
                    [color=blue]
                    > 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:
                    >[color=green]
                    > > 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:
                    > >[color=darkred]
                    > > > 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>[/color][/color][/color]

                    Comment

                    Working...