locked
Help convert VB LINQ to XML to C# RRS feed

  • Question

  • Hello,
    I have this method in VB and I am rewriting my program in C#. I need some help with conversion.
    Here is my xml;
    <SiteInformation>
     Other elements.......
      <TaskHeaders>
        <Header Owner="ProjectOwner" Name="Setup">Package Setup</Header>
        <Header Owner="Packager" Name="Creation">Package Creation</Header>
        <Header Owner="QController" Name="QCT">Quality Control Testing</Header>
        <!--<Header Owner="QController" Name="QC">Quality Testing</Header>-->
        <Header Owner="ProjectOwner" Name="UAT">Requestor Testing and Approval</Header>
        <Header Owner="ProjectOwner" Name="Release">Release Management</Header>
      </TaskHeaders>
    </SiteInformation>
    Here is the method I'm trying to convert.
            Public Shared Sub SetTaskHeaders()
                Dim lstHeaders As New Collections.Hashtable
                Try
                    xmlDoc = XDocument.Load(Application.StartupPath & "\pra.xml")
                    Dim Query = From el In xmlDoc.<Root>.<TaskHeaders>
                                Select el.Elements
                    For Each el In Query(0)
                        lstHeaders.Add(el.Value.ToString, el.Attribute("Owner").Value)
                    Next
                    TaskHeaders = lstHeaders
                Catch ex As ApplicationException
                    Throw
                End Try
            End Sub
    I have tried searching for the proper syntax but i am noob. Any help would be much appreciated. 
    Wednesday, August 10, 2011 2:19 PM

Answers

  • xmlDoc = XDocument.Load(Application.StartupPath + "\\pra.xml")

    foreach (XElement header in doc.Root.Elements("TaskHeaders").Elements("Header")) {

      lstHeaders.Add(header.Value, header.Attribute("Owner").Value)

    }

    should probably do what you want although I have not attempted a statement by statement translation of the VB code, rather I have tried to understand which data you want to extract to then implement that with C#.

    Note that your VB sample does not make sense with the XML sample you have posted as it does xmlDoc.<Root> yet your sample has no element named "Root". So I have assumed that to do xml.Root instead to access the property named "Root" of your XDocument.


    MVP Data Platform Development My blog
    Wednesday, August 10, 2011 4:18 PM

All replies

  • xmlDoc = XDocument.Load(Application.StartupPath + "\\pra.xml")

    foreach (XElement header in doc.Root.Elements("TaskHeaders").Elements("Header")) {

      lstHeaders.Add(header.Value, header.Attribute("Owner").Value)

    }

    should probably do what you want although I have not attempted a statement by statement translation of the VB code, rather I have tried to understand which data you want to extract to then implement that with C#.

    Note that your VB sample does not make sense with the XML sample you have posted as it does xmlDoc.<Root> yet your sample has no element named "Root". So I have assumed that to do xml.Root instead to access the property named "Root" of your XDocument.


    MVP Data Platform Development My blog
    Wednesday, August 10, 2011 4:18 PM
  • Thx for the response and your correct. I did not grab the <Root> element with the copy of the XML.

    It is suppose to be like this;

    <Root>

      <SiteInformation>

     

       Other elements.......
        <TaskHeaders>
          <Header Owner="ProjectOwner" Name="Setup">Package Setup</Header>
          <Header Owner="Packager" Name="Creation">Package Creation</Header>
          <Header Owner="QController" Name="QCT">Quality Control Testing</Header>
          <!--<Header Owner="QController" Name="QC">Quality Testing</Header>-->
          <Header Owner="ProjectOwner" Name="UAT">Requestor Testing and Approval</Header>
          <Header Owner="ProjectOwner" Name="Release">Release Management</Header>
        </TaskHeaders>
      </SiteInformation>
    </Root>
     
    I will try this out and let you know if I have any issues.
    thx again,

    Mike

    Wednesday, August 10, 2011 7:01 PM
  • Thx, Martin.

     

    This worked just as I expected it too.


    Mike
    Thursday, August 11, 2011 1:55 PM