Adding in Child elements depending on linq to Sql field values
-
I have a problem I hope someone can help me with - firstly I am new to Linq and everything I know so far I have read up on the net... My problem is this, I am using the code below to return records from a function in SQL, I now want to use the XElement to create my xml structure which works fine, however in my structure I may have multiple elements of say vehicle, is there a way to add child elements belonging to Vehichles/vehicle? Or do I have to create separate calls to handle and then use the Xelement.Add? The addfirst is the closest function I have found but it obviously doesn't create the child element in the correct place i.e. under Vehicles/Vehicle in the xml Dim _db As New LiquidDataContext Dim _client = From C In _db.GetPolicyInformation("LIQ1414") Dim x As String Dim _xml As New Xml.Linq.XDocument Dim _xp As New XElement("Clients") For Each _c In _client _xp.Add(<Policies> <Client> <Name><%= _c.Name %></Name> <IDNumber><%= _c.IDNumber %></IDNumber> <ClientContact> <HomeTelNo><%= _c.HomeTelNo %></HomeTelNo> <WorkTelNo><%= _c.WorkTelNo %></WorkTelNo> </ClientContact> </Client> <Policy> <PolicyNumber><%= _c.DisplayPolicyNo %></PolicyNumber> <Balance><%= _c.Balance %></Balance> <Vehicles> <Vehicle> <SumInsured><%= _c.SumInsured %></SumInsured> <Premium><%= _c.Premium %></Premium> </Vehicle> </Vehicles> </Policy> </Policies>) Next _xml.Add(_xp) _xml.Save("C:\Policy.xml") Thanks in anticipation - ps Excuse the "&" ;)
-
I have a problem I hope someone can help me with - firstly I am new to Linq and everything I know so far I have read up on the net... My problem is this, I am using the code below to return records from a function in SQL, I now want to use the XElement to create my xml structure which works fine, however in my structure I may have multiple elements of say vehicle, is there a way to add child elements belonging to Vehichles/vehicle? Or do I have to create separate calls to handle and then use the Xelement.Add? The addfirst is the closest function I have found but it obviously doesn't create the child element in the correct place i.e. under Vehicles/Vehicle in the xml Dim _db As New LiquidDataContext Dim _client = From C In _db.GetPolicyInformation("LIQ1414") Dim x As String Dim _xml As New Xml.Linq.XDocument Dim _xp As New XElement("Clients") For Each _c In _client _xp.Add(<Policies> <Client> <Name><%= _c.Name %></Name> <IDNumber><%= _c.IDNumber %></IDNumber> <ClientContact> <HomeTelNo><%= _c.HomeTelNo %></HomeTelNo> <WorkTelNo><%= _c.WorkTelNo %></WorkTelNo> </ClientContact> </Client> <Policy> <PolicyNumber><%= _c.DisplayPolicyNo %></PolicyNumber> <Balance><%= _c.Balance %></Balance> <Vehicles> <Vehicle> <SumInsured><%= _c.SumInsured %></SumInsured> <Premium><%= _c.Premium %></Premium> </Vehicle> </Vehicles> </Policy> </Policies>) Next _xml.Add(_xp) _xml.Save("C:\Policy.xml") Thanks in anticipation - ps Excuse the "&" ;)
Can you please reformat you code using the <pre> tags? I can hardly read it with all the escaped XML chars.
modified on Monday, September 22, 2008 11:48 AM
-
Can you please reformat you code using the <pre> tags? I can hardly read it with all the escaped XML chars.
modified on Monday, September 22, 2008 11:48 AM
Apologies for that - I tried to keep it as close to the orginal as possible, below is the code with correct format...please note the field values in the XML have the prefix characters taken out. The problem remains the same, I cannot add child elements belonging to Vehicles with the code in this structure and the logic I require means I need to add multiple child blocks i.e not just to Vehicles eg Buildings - any help would be appreciated!
Dim \_db As New LiquidDataContext Dim \_client = From C In \_db.GetPolicyInformation("1414") Dim \_xml As New Xml.Linq.XDocument Dim \_xp As New XElement("Clients") For Each \_c In \_client \_xp.Add(<Client> <Name>\_c.Name</Name> <IDNumber>\_c.IDNumber</IDNumber> <ClientContact> <HomeTelNo>\_c.HomeTelNo</HomeTelNo> <WorkTelNo>\_c.WorkTelNo</WorkTelNo> </ClientContact> <Policy> <PolicyNumber>\_c.DisplayPolicyNo</PolicyNumber> <Balance>\_c.Balance</Balance> <InceptionDate>\_c.InceptionDate</InceptionDate> <RenewalDate>2009/05/30</RenewalDate> <Vehicles> <Vehicle> <MakeDesc>\_c.MakeDesc</MakeDesc> <Model>\_c.ModelDesc</Model> </Vehicle> </Vehicles> </Policy> </Client>) Next \_xml.Add(\_xp) \_xml.Save("C:\\Client.xml")
Thanks in advance - Sean