Results 1 to 2 of 2

Thread: Update XML file.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Update XML file.

    Hello,

    I'm trying to enter new information in a specific node in a xml file. This is the structure of the xml file:

    <Root>
    <Title 1>
    <Title 2></Title 2>
    </Title 1>
    </Root>

    How would I update <Title 2></Title 2> field?

    Thanks in advance.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Update XML file.

    This was created in VS2008, should work just fine in VS2010.

    Create a file called Books.xml in the executable folder.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Root>
        <Titles>
            <Title Name="1">I love .NET</Title>
            <Title Name="2">I Love Delphi</Title>
        </Titles>
    </Root>
    Add this code

    Code:
        Private Sub UpdateXmlItem()
            Dim Doc = XDocument.Load("Books.xml")
            Dim query = From items In Doc...<Title> Where items.@Name = "2"
    
            If query.Value = "Bye Bye Delphi" Then
                Console.WriteLine("You already said good bye")
            Else
                Console.WriteLine(query.Value)
                query.Value = "Bye Bye Delphi"
    
                Console.WriteLine(query.Value)
                Doc.Save("Books.xml")
            End If
    
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width