Results 1 to 6 of 6

Thread: LINQ to XML issue

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    LINQ to XML issue

    hi Guys,

    I have the following code:

    Code:
    Dim doc As XDocument = New XDocument( _
                    New XDeclaration("1.0", "utf-8", "true"), _
                    New XElement("LeanerFirstTimeRegistration", _
                                 New XElement("Header", _
                                 New XElement("Record", New XAttribute("Type", "1"), _
                                 New XElement("Filename", "test"), _
                                 New XElement("ProvinceID", "5"), _
                                 New XElement("EmisNumber", ""), _
                                 New XElement("StatusID", "1"), _
                                 New XElement("TransactionCategoryID", "4"), _
                                 New XElement("SequenceNumber", "1"), _
                                 New XElement("DateCreated", Format(Now, "yyyy-MM-dd")), _
                                 New XElement("CreatedBy", "me"))), _
              New XElement("LearnerInformation", _
                From people As DataRow In table.Rows _
                Order By people("LearnerSurname") _
                Select New XElement("LeanerBiographical", _
                New XElement("Record", _
                New XAttribute("Type", "2"), _
                  New XElement("ProvinceID", "5"), _
                  New XElement("LearnerNumber", ""), _
                  New XElement("EmisNumber", ""), _
                  New XElement("Year", Now.Year), _
                  New XElement("LearnerSurname", people("LearnerSurname")), _
                  New XElement("LearnerName1", people("LearnerName1")), _
                  New XElement("LearnerName2", people("LearnerName2")), _
                  New XElement("LearnerName3", people("LearnerName3")), _
                  New XElement("BirthDate", people("BirthDate")), _
                  New XElement("GenderID", people("GenderID")), _
                  New XElement("ParentNumber1", people("ParentNumber1")), _
                  New XElement("ParentNumber2", people("ParentNumber2")), _
                  New XElement("GradeID", people("GradeID")), _
                  New XElement("Class", people("class")) _
                    )))))
    
    
                doc.Save("C:\mynewXmFile.xml")
    here is a portion of the data generated:

    Code:
      <?xml version="1.0" encoding="utf-8" ?> 
    - <LeanerFirstTimeRegistration>
    - <Header>
    - <Record Type="1">
      <Filename>test</Filename> 
      <ProvinceID>5</ProvinceID> 
      <EmisNumber></EmisNumber> 
      <StatusID>1</StatusID> 
      <TransactionCategoryID>4</TransactionCategoryID> 
      <SequenceNumber>1</SequenceNumber> 
      <DateCreated>2009-10-20</DateCreated> 
      <CreatedBy>me</CreatedBy> 
      </Record>
      </Header>
    - <LearnerInformation>
    - <LeanerBiographical>
    - <Record Type="2">
      <ProvinceID>5</ProvinceID> 
      <LearnerNumber /> 
      <EmisNumber /> 
      <Year>2009</Year> 
      <LearnerSurname>AFOL</LearnerSurname> 
      <LearnerName1>name1</LearnerName1> 
      <LearnerName2>name2</LearnerName2> 
      <LearnerName3>name3</LearnerName3> 
      <BirthDate>20010601</BirthDate> 
      <GenderID>1</GenderID> 
      <ParentNumber1>0</ParentNumber1> 
      <ParentNumber2>776</ParentNumber2> 
      <GradeID>3</GradeID> 
      <Class>Grade 3H</Class> 
      </Record>
      </LeanerBiographical>
    now I need to add a node after LeanerBiographical like this:

    Code:
    </LearnerBiographical>
    - <LearnerActivity>
    - <Record type="3">
      <EmisNumber>500000006</EmisNumber> 
      <LearnerNumber /> 
      <Year>2007</Year> 
      <ExtraCurricularActivitiesID>2</ExtraCurricularActivitiesID> 
      </Record>
      </LearnerActivity>
    how can I achieve this?

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: LINQ to XML issue

    Hey,

    I take it that you need to add this node after you have first created the XML file, is that correct?

    Or, why don't you just add that node when you are creating the XML file initially?

    Gary

  3. #3

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: LINQ to XML issue

    hi gary,

    i want to add it in the code i posted, while the file is being created. but i'm not usre where exactlyit needs to go.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: LINQ to XML issue

    Hey,

    So what am I missing here....

    You have a For Loop which creates the entries in the LearnerInformation Node, once that For Loop has finished you want to start creating another node, right? So you would begin creating new elements there.

    Gary

  5. #5

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: LINQ to XML issue

    hi gary,

    when i add the code as follows for the new element i get an error:

    [CODE] New XElement("LearnerInformation", _
    From people As DataRow In table.Rows _
    Order By people("LearnerSurname") _
    Select New XElement("LearnerBiographical", _
    New XElement("Record", _
    New XAttribute("Type", "2"), _
    New XElement("ProvinceID", "5"), _
    New XElement("LearnerNumber", ""), _
    New XElement("EmisNumber", ""), _
    New XElement("Year", Now.Year), _
    New XElement("LearnerSurname", people("LearnerSurname")), _
    New XElement("LearnerName1", people("LearnerName1")), _
    New XElement("LearnerName2", people("LearnerName2")), _
    New XElement("LearnerName3", people("LearnerName3")), _
    New XElement("BirthDate", people("BirthDate")), _
    New XElement("GenderID", people("GenderID")), _
    New XElement("ParentNumber1", people("ParentNumber1")), _
    New XElement("ParentNumber2", people("ParentNumber2")), _
    New XElement("GradeID", people("GradeID")), _
    New XElement("Class", people("class")) _
    )), New XElement("dd", "dd"))))[/CODE]

    to me that seems like the right place for it based on what i need. i get this error:

    Code:
    Range variable name can be inferred only from a simple or qualified name with no arguments.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: LINQ to XML issue

    Hey,

    I think I see where the problem is coming now, although I am not sure how exactly you fix it, as unfortunately, my LINQ is quite poor, only really starting to play with it.

    Essentially, you are using a LINQ statement to find all the Rows in your DataTable, and for each one, create a LearnerInformation Node, then you want to do the same thing again, to create a LearnerActivity node, which I am assuming has information contained within your DataTable again. However, for clarity sake, you want that node to appear underneath the LearnerInformation node, and it is this part that I am not sure how you could do. I can see how it could be constructed if you weren't using LINQ, but...

    Gary

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