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. Adding in Child elements depending on linq to Sql field values

Adding in Child elements depending on linq to Sql field values

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaselinqxml
3 Posts 2 Posters 2 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.
  • S Offline
    S Offline
    Sean75
    wrote on last edited by
    #1

    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 "&" ;)

    J 1 Reply Last reply
    0
    • S Sean75

      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 "&" ;)

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        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

        S Offline
        S Offline
        Sean75
        wrote on last edited by
        #3

        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

        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