Linq to Xml Programming
-
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 PropertyPrivate \_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()
-
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 PropertyPrivate \_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()
-
My apologies, I didn't quite know where to post it as the question covers both fields. Still stuck though.
JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************
-
My apologies, I didn't quite know where to post it as the question covers both fields. Still stuck though.
JimBob SquarePants ******************************************************************* "He took everything personally, including our royalties!" David St.Hubbins, Spinal Tap about Ian Faith, their ex-manager *******************************************************************
Your question involves LINQ usage. Thus, you should post it under the .NET Framework forum. Since it is written in Visual Basic, you could have place it there as a second choice. However, you should still only post it in one forum.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
modified on Saturday, March 28, 2009 5:11 PM