Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Linq to XML programming

Linq to XML programming

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqxmlannouncement
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JimBob SquarePants
    wrote on last edited by
    #1

    Hi there, I'm trying to teach myself linq to xml and I've been struggling to implement a linq to xml class with which I can update values in a given xml file. I'm a total beginner at this so I have probably made a simple mistake. My Base class is as follows:

    Public Class WebSite
    Private _Id As String
    Public Property Id() As String
    Get
    Return (_Id)
    End Get
    Set(ByVal value As String)
    _Id = value
    End Set
    End Property
    Private _Link As String
    Public Property Link() As String
    Get
    Return (_Link)
    End Get
    Set(ByVal value As String)
    _Link = value
    End Set
    End Property

    Private \_Picture As String
    Public Property Picture() As String
        Get
            Return (\_Picture)
        End Get
        Set(ByVal value As String)
            \_Picture = value
        End Set
    End Property
    
    Private \_ModifiedOn As String
    Public Property ModifiedOn() As String
        Get
            Return (\_ModifiedOn)
        End Get
        Set(ByVal value As String)
            \_ModifiedOn = value
        End Set
    End Property
    
    Public Sub New(ByVal xElement As XElement)
        Id = xElement.Attribute("Id").Value
        Link = xElement.Element("Link").Value
        Picture = xElement.Element("Picture").Value
        ModifiedOn = xElement.Element("ModifiedOn").Value
    End Sub
    Private \_xElement As XElement
    Public Property xElement() As XElement
        Get
            Return New XElement("Website", New XAttribute("Id", Id), New XElement("Link", Link), New XElement("Picture", Picture), New XElement("ModifiedOn", ModifiedOn))
    
        End Get
        Set(ByVal value As XElement)
            \_xElement = value 
        End Set
    End Property
    

    The sub that I am trying to implement is as follows:

    Public Class Portfolio
    Inherits List(Of WebSite)

    Public Sub UpdateWebsite(ByVal xmlFile As String, ByVal WebId As String, ByVal WebLink As String, ByVal WebPic As String, ByVal WebMod As String)
        Dim doc As XDocument = XDocument.Load(xmlFile)
        Dim query = From xElem In doc.Descendants("Website") \_
                    Where xElem.@Id = WebId \_
        Select New WebSite(xElem)
    
    
        With query.SingleOrDefault
            .Id = WebId
            .Link = WebLink
            .Picture = WebPic
            .ModifiedOn = WebMod
        End With
    
        Me.Clear()
    
    G 1 Reply Last reply
    0
    • J JimBob SquarePants

      Hi there, I'm trying to teach myself linq to xml and I've been struggling to implement a linq to xml class with which I can update values in a given xml file. I'm a total beginner at this so I have probably made a simple mistake. My Base class is as follows:

      Public Class WebSite
      Private _Id As String
      Public Property Id() As String
      Get
      Return (_Id)
      End Get
      Set(ByVal value As String)
      _Id = value
      End Set
      End Property
      Private _Link As String
      Public Property Link() As String
      Get
      Return (_Link)
      End Get
      Set(ByVal value As String)
      _Link = value
      End Set
      End Property

      Private \_Picture As String
      Public Property Picture() As String
          Get
              Return (\_Picture)
          End Get
          Set(ByVal value As String)
              \_Picture = value
          End Set
      End Property
      
      Private \_ModifiedOn As String
      Public Property ModifiedOn() As String
          Get
              Return (\_ModifiedOn)
          End Get
          Set(ByVal value As String)
              \_ModifiedOn = value
          End Set
      End Property
      
      Public Sub New(ByVal xElement As XElement)
          Id = xElement.Attribute("Id").Value
          Link = xElement.Element("Link").Value
          Picture = xElement.Element("Picture").Value
          ModifiedOn = xElement.Element("ModifiedOn").Value
      End Sub
      Private \_xElement As XElement
      Public Property xElement() As XElement
          Get
              Return New XElement("Website", New XAttribute("Id", Id), New XElement("Link", Link), New XElement("Picture", Picture), New XElement("ModifiedOn", ModifiedOn))
      
          End Get
          Set(ByVal value As XElement)
              \_xElement = value 
          End Set
      End Property
      

      The sub that I am trying to implement is as follows:

      Public Class Portfolio
      Inherits List(Of WebSite)

      Public Sub UpdateWebsite(ByVal xmlFile As String, ByVal WebId As String, ByVal WebLink As String, ByVal WebPic As String, ByVal WebMod As String)
          Dim doc As XDocument = XDocument.Load(xmlFile)
          Dim query = From xElem In doc.Descendants("Website") \_
                      Where xElem.@Id = WebId \_
          Select New WebSite(xElem)
      
      
          With query.SingleOrDefault
              .Id = WebId
              .Link = WebLink
              .Picture = WebPic
              .ModifiedOn = WebMod
          End With
      
          Me.Clear()
      
      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      JimBob SquarePants wrote:

      With query.SingleOrDefault
      .Id = WebId
      .Link = WebLink
      .Picture = WebPic
      .ModifiedOn = WebMod
      End With

      Me.Clear()
      AddRange(query)

      Save(xmlFile)

      This is your problem. The query will be enumerated once to get the SingleOrDefault. The returned item will be modified and then thrown away at the End With. The query will be enumerated again (making a new set of Websites) when you call AddRange.

      J 1 Reply Last reply
      0
      • G Gideon Engelberth

        JimBob SquarePants wrote:

        With query.SingleOrDefault
        .Id = WebId
        .Link = WebLink
        .Picture = WebPic
        .ModifiedOn = WebMod
        End With

        Me.Clear()
        AddRange(query)

        Save(xmlFile)

        This is your problem. The query will be enumerated once to get the SingleOrDefault. The returned item will be modified and then thrown away at the End With. The query will be enumerated again (making a new set of Websites) when you call AddRange.

        J Offline
        J Offline
        JimBob SquarePants
        wrote on last edited by
        #3

        So what do you suggest? When I debug the code I can see that none of the values are being updated even before I reach the end of the with. I just cant figure it out? Many Thanks

        JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

        L G 2 Replies Last reply
        0
        • J JimBob SquarePants

          So what do you suggest? When I debug the code I can see that none of the values are being updated even before I reach the end of the with. I just cant figure it out? Many Thanks

          JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Hey dude, I followed this here from the cross post reply. I can't help you with linq because I have never used it, yet. Not sure when I will start but it seems inevitable. However since you said you were still stuck I can speak to being a beginner and stuck since I of course was there often myself. One suggestion is to abandon your specific goal for now and go back to just studying LINQ. What I find is doing so will almost always result in gaining an understanding of the technology and ultimately I am able to return to the original task and solve the problem.

          J 1 Reply Last reply
          0
          • J JimBob SquarePants

            So what do you suggest? When I debug the code I can see that none of the values are being updated even before I reach the end of the with. I just cant figure it out? Many Thanks

            JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            How exactly are you checking the values as you debug? If you are adding "query.SingleOrDefault" to the watch window, then it will reenumerate the collection every time you refresh it in the debug window, thus giving you the original data. I would suggest having a Load method that loads the files into the list and have the UpdateWebsite method find the matching website from the loaded sites. Also, you are using VB, so you should take advantage of the XML literals and XLinq syntax. You could change the WebSite constructor and xElement property getter to look like this:

            Public Sub New(ByVal xElement As XElement)
            Id = xElement.@Id
            Link = xElement.<Link>.Value
            Picture = xElement.<Picture>.Value
            ModifiedOn = xElement.<ModifiedOn>.Value
            End Sub
            Private _xElement As XElement
            Public Property xElement() As XElement
            Get
            Return <Website Id=<%= Me.Id %>>
            <Link><%= Me.Link %></Link>
            <Picture><%= Me.Picture %></Picture>
            <ModifiedOn><%= Me.ModifiedOn %></ModifiedOn>
            </Website>
            End Get
            Set(ByVal value As XElement)
            _xElement = value
            End Set
            End Property

            J 1 Reply Last reply
            0
            • G Gideon Engelberth

              How exactly are you checking the values as you debug? If you are adding "query.SingleOrDefault" to the watch window, then it will reenumerate the collection every time you refresh it in the debug window, thus giving you the original data. I would suggest having a Load method that loads the files into the list and have the UpdateWebsite method find the matching website from the loaded sites. Also, you are using VB, so you should take advantage of the XML literals and XLinq syntax. You could change the WebSite constructor and xElement property getter to look like this:

              Public Sub New(ByVal xElement As XElement)
              Id = xElement.@Id
              Link = xElement.<Link>.Value
              Picture = xElement.<Picture>.Value
              ModifiedOn = xElement.<ModifiedOn>.Value
              End Sub
              Private _xElement As XElement
              Public Property xElement() As XElement
              Get
              Return <Website Id=<%= Me.Id %>>
              <Link><%= Me.Link %></Link>
              <Picture><%= Me.Picture %></Picture>
              <ModifiedOn><%= Me.ModifiedOn %></ModifiedOn>
              </Website>
              End Get
              Set(ByVal value As XElement)
              _xElement = value
              End Set
              End Property

              J Offline
              J Offline
              JimBob SquarePants
              wrote on last edited by
              #6

              Thanks for your help! There always seems so be so many ways to solve a problem when programming, I fear for the sanity of anyone who does this professionally. I followed led mikes advice and did a bit more studying and I seem to have cracked it. Just built a fully funtional content management website with membership et al in just over 20 hours! feeling quite pleased with myself. I can definitely see the benefits of using xml for smaller traffic sites rather than a database. I'll have a good look at your code and see i can learn more. I'm really starting to get into all this. Many thanks James

              JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

              1 Reply Last reply
              0
              • L led mike

                Hey dude, I followed this here from the cross post reply. I can't help you with linq because I have never used it, yet. Not sure when I will start but it seems inevitable. However since you said you were still stuck I can speak to being a beginner and stuck since I of course was there often myself. One suggestion is to abandon your specific goal for now and go back to just studying LINQ. What I find is doing so will almost always result in gaining an understanding of the technology and ultimately I am able to return to the original task and solve the problem.

                J Offline
                J Offline
                JimBob SquarePants
                wrote on last edited by
                #7

                Hopefully you'll read this. Thanks for your input. I followed your advice and started studying again with great success. I figured out all my problems and solved a few new ones one the way. I looked at linq first cos quie frankly the whole ado.net thing scares me. It seems for the most part to make pretty good sense. I'm looking forward to learning more. Thanks again James

                JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

                L 1 Reply Last reply
                0
                • J JimBob SquarePants

                  Hopefully you'll read this. Thanks for your input. I followed your advice and started studying again with great success. I figured out all my problems and solved a few new ones one the way. I looked at linq first cos quie frankly the whole ado.net thing scares me. It seems for the most part to make pretty good sense. I'm looking forward to learning more. Thanks again James

                  JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  JimBob SquarePants wrote:

                  Hopefully you'll read this.

                  I did. Thanks for the update. I am glad things are moving along for you now.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups