Results 1 to 9 of 9

Thread: [2005] Reading XML File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    262

    [2005] Reading XML File

    Hello again.. I am starting to work with XML files and haven't ever really done this. I started searching on how to read through xml files but I can't quite grasp what I need to do. My goal is to write a new aspx page at runtime based on the values in the xml file

    Here is the XML file minus some of the tests to save space

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <HealthReport>
      
      <Test>
        <NumberOfResponses>5</NumberOfResponses>
        <TestSection>ServiceOverview</TestSection>
        <TestTitle></TestTitle>
        <TableData>
          <LeftColumnName>Service</LeftColumnName>
          <RightColumnName>Result</RightColumnName>
        </TableData>
        <Option>
          <TestText>ASP</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Games</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>MSS</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Music</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>MAPS</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
      </Test>
    
      <Test>
        <NumberOfResponses>5</NumberOfResponses>
        <TestSection>Games</TestSection>
        <TestTitle>RealArcade Information</TestTitle>
        <URLText>source: http://www.realarcade.com/gameguide </URLText>
        <URL>http://www.realarcade.com/gameguide</URL>
        <URLText>source: http://www.realarcade.com/game pass Trial 1 </URLText>
        <URL>http://www.realarcade.com/gamepass?fg=true&amp;tps=cj_&amp;src=cj,2fg_,mcode_%zp%7C%za_</URL>
        <URLText>source: http://www.realarcade.c om/gamepass Trial 2 </URLText>
        <URL>http://www.realarcade.com/gamepass?gptype=trial&amp;tps=cj_&amp;src=cj,trial_,mcode_%zp%7C%za_</URL>
        <URLText>source: http://www.realarcade.com Mysteryville Promotion </URLText>
        <URL>http://www.realarcade.com/promoGamePass?gameid=mysteryville&amp;tps=cj_&amp;ptype=2t&amp;src=cj,trial_,d2g_,mcode_%zp%7C%za_</URL>
        <URLText>source: http://www.realarcade.com Burger Rush Promotion </URLText>
        <URL>http://www.realarcade.com/promoGamePass?gameid=burgerrush&amp;tps=cj_&amp;ptype=2t&amp;src=cj,trial_,d2g_,mcode_%zp%7C%za_</URL>
        <TableData>
          <LeftColumnName>Action</LeftColumnName>
          <RightColumnName>Result</RightColumnName>
        </TableData>
        <Option>
          <TestText>Open Client</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Sign In / Sign Out</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Download Game</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Launch Game</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Check Reference Links</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
      </Test>
    
       <Test>
        <NumberOfResponses>5</NumberOfResponses>
        <TestSection></TestSection>
        <TestTitle>Superpass</TestTitle>
        <URLText>source: http://superpass.real.com </URLText>
        <URL>http://superpass.real.com</URL>
        <TableData>
          <LeftColumnName>Action</LeftColumnName>
          <RightColumnName>Result</RightColumnName>
        </TableData>
        <Option>
          <TestText>Download RealPlayer Client</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Open SP home</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Sign In / Sign Out</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Verify Slideshow</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
        <Option>
          <TestText>Validate a 24/7 Stream</TestText>
          <ResponeType>Radio</ResponeType>
          <CodeFileRef></CodeFileRef>
        </Option>
      </Test>
    
      <!-- Next section to add is Rhapsody Direct - Sonos -->
    
    </HealthReport>
    then after it reads it, i want the data displayed on an aspx page in tables w/ the associated controls

    Code:
    <table class="vertical listing" border="1" style="width: 219px"><tbody><tr><td><p align="center" 
    
    style="text-align: center;"><b>Service</b></p></td><td style="width: 157px"><p align="center" style="text-align: 
    
    center;"><b>Result</b></p></td></tr><tr><td><p align="center">ASP</p></td><td style="width: 157px">
            <asp:RadioButtonList ID="rbSOAsp" runat="server" Height="1px" RepeatDirection="Horizontal" Width="140px">
                <asp:ListItem>Pass</asp:ListItem>
                <asp:ListItem>Fail</asp:ListItem>
            </asp:RadioButtonList>
    </td></tr><tr><td><p align="center">Games</p></td><td style="width: 157px">
            <asp:RadioButtonList ID="rbSOGames" runat="server" Height="1px" RepeatDirection="Horizontal" Width="140px">
                <asp:ListItem>Pass</asp:ListItem>
                <asp:ListItem>Fail</asp:ListItem>
            </asp:RadioButtonList>
    </td></tr><tr><td><p align="center">MS&amp;S</p></td><td style="width: 157px">
            <asp:RadioButtonList ID="rbSOMSS" runat="server" Height="1px" RepeatDirection="Horizontal" Width="140px">
                <asp:ListItem>Pass</asp:ListItem>
                <asp:ListItem>Fail</asp:ListItem>
            </asp:RadioButtonList>
    </td></tr><tr><td><p align="center">Music</p></td><td style="width: 157px">        
            <asp:RadioButtonList ID="rbSOMusic" runat="server" Height="1px" RepeatDirection="Horizontal" Width="140px">
                <asp:ListItem>Pass</asp:ListItem>
                <asp:ListItem>Fail</asp:ListItem>
            </asp:RadioButtonList></td></tr><tr><td>
            <p align="center">MAPS</p></td><td style="width: 157px">
            <asp:RadioButtonList ID="rbSOMaps" runat="server" Height="1px" RepeatDirection="Horizontal" Width="140px">
                <asp:ListItem>Pass</asp:ListItem>
                <asp:ListItem>Fail</asp:ListItem>
            </asp:RadioButtonList>
    </td></tr></tbody></table>

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

    Re: [2005] Reading XML File

    Hey,

    There are multiple ways of reading in the XML Document and parsing through it, but my preferred way of doing it is using XPath.

    Assuming you have an xml file that you can get access to, then the following would load all the Test nodes into a list that you can loop through and do something with the data.

    Code:
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("test.xml");
    
    XmlNodeList nodes = xmlDoc.SelectNodes("/HealthReport/Test");
    
    foreach(XmlNode node in nodes)
    {
           //Do something with the value here
    }
    This is a very simple example, but XPath Queries can be made as complicated as you want for plucking out the exact information you want.

    In terms of loading the information onto an aspx page, you can either load the controls at design time, and then assign their properties based on what you take out the XML, or you could create the controls at run time.

    Maybe work on getting the information out the XML file, then you can worry about displaying all the information in the exact way you want it.

    Hope this helps!!

    Gary

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    262

    Re: [2005] Reading XML File

    I am still confused about identifying the parts of the xml - i am also using VB, but I think i did a similar conversion of the code -- here is a sample of what i am looking for - i load the xmldoc, read the first test, find it has 5 answers so a 5 row table is created and then the table data is completed based on the rest of the test info.. here is what i did to try and get the info out of the xmldoc but only get a value of System.Xml.XPathNodeList

    Code:
        Public Sub SetupReport()
            Dim nodeArray As String()
    
            Try
                Dim doc As XmlDocument
                Dim nodes As XmlNodeList
    
                doc = New XmlDocument
    
                doc.Load("C:\ReportTests.xml")
                nodes = doc.SelectNodes("/HealthReport/Test")
    
                Dim x As Integer
    
                For x = 0 To (nodes.Count - 1)
                    ReDim Preserve nodeArray(x)
                    nodeArray(x) = nodes.ToString
                Next
    
            Catch ex As Exception
    
            End Try
        End Sub

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

    Re: [2005] Reading XML File

    Hello,

    The ToString method, performed on an XmlNodeList will only return the Type of that object, hence the System.Xml.XPathNodeList.

    Based on the sample you have posted, nodes contains all of the Test Xml Nodes that is found in XML File. i.e NumberOfResponses etc.

    In order to make use of the information that is within the nodes, you have to tell it what information you actually want. This could be done by doing additional XPath querys on each node, for instance, in order to extract the fact that the first Test has 5 responses:

    Code:
                For Each node As XmlNode In nodes
                    Dim responses As Integer = CInt(node.SelectSingleNode("/NumberOfResponses").InnerText)
                Next
    Gary

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    262

    Re: [2005] Reading XML File

    sorry for being needy, but how would i get the options and their corresponding fields out?

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

    Re: [2005] Reading XML File

    Expanding on what was given in the previous posts, what you are wanting to get are a list of all the Option nodes, so we are going to need an XmlNodeList, and we want all the Option Nodes under the current Test Node, therefore you could use something like:

    Code:
    Dim optionNodes As XmlNodeList
    optionNodes = node.SelectNodes("/Option")
    
    For Each optionNode As XmlNode In optionNodes
        Dim TestText As String = optionNode.SelectSingleNode("/TestText").InnerText
        Dim ResponeType As String  = optionNode.SelectSingleNode("/ResponeType ").InnerText
        Dim CodeFileRef As String  = optionNode.SelectSingleNode("/CodeFileRef ").InnerText
    Next
    Make sense?

    Gary

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    262

    Re: [2005] Reading XML File

    alright so i am able to extract the data and for the most part redisplay it.. there is still one little problem still.. it seems i will have to set all the style properties in the response otherwise it sets an absolute postion and everything stacks up in the top left corner

    Code:
            Try
                Dim doc As XmlDocument
                Dim tnodes As XmlNodeList
                Dim oNodes As XmlNodeList
                Dim section As String, title As String
                Dim header1 As String, header2 As String
    
                doc = New XmlDocument
                'Load the tests xml
                doc.Load("C:\ReportTests.xml")
                tnodes = doc.SelectNodes("/HealthReport/Test")
    
                Response.Write("<table border='1' style='width: 219px'><tbody><tr><td><p align='center'")
                Response.Write("style='text-align: center;'><b>Service</b></p></td><td style='width: 157px'><p align='center' style='text-align: center;'>")
                Response.Write("<b>Result</b></p></td></tr><tr><td><p align='center'>ASP</p></td><td style='width: 157px'>")
                Response.Write("<asp:RadioButtonList ID='rbSOAsp' runat='server' Height='1px' RepeatDirection='Horizontal' Width='140px'>")
                Response.Write("<asp:ListItem>Pass</asp:ListItem>")
                Response.Write("<asp:ListItem>Fail</asp:ListItem>")
                Response.Write("</asp:RadioButtonList>")
                Response.Write("</td></tr></tbody></table>")
                Dim node As XmlNode
    
                For Each node In tnodes
    
                    Dim responses As Integer = CInt(node.SelectSingleNode("/NumberOfResponses").InnerText)
    
                    If Not responses <= 0 Then
                        'Access the section for the test
                        section = node.SelectSingleNode("/TestSection").InnerText
                        'Access the test title
                        title = node.SelectSingleNode("/TestTitle").InnerText
                        'Access the Left table header
                        header1 = node.SelectSingleNode("/TableData/LeftColumnName").InnerText
                        'Access the Right table header
                        header2 = node.SelectSingleNode("/TableData/RightColumnName").InnerText
    
                        'Write the initial table
                        Response.Write("<P style='text-align: left'>" & title & "</P>")
                        Response.Write("<BR /><BR />")
                        Response.Write("<P style='text-align: left'>" & title & "</P>")
    
                        'Gather the options
                        oNodes = node.SelectNodes("/Option")
    
                        'Write each option to the table
                        For Each node2 As XmlNode In oNodes
                            Dim TestText As String = node2.SelectSingleNode("/TestText").InnerText
                            Dim ResponseType As String = node2.SelectSingleNode("/ResponseType").InnerText
                        Next
    
                    End If
                Next
    
            Catch ex As Exception
    
            End Try

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

    Re: [2005] Reading XML File

    Hey,

    Although using Response.Write will work, from what I understand, using it is frowned upon, and should be avoided where possible.

    You might want to into dynamically adding controls during the initialization of the page.

    A quick google through up lots of examples of how this can be done.

    Gary

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Reading XML File

    Generate your string with the HTML in it and give it to a Literal's TEXT property.

    Also, you can't write out a "<asp:RadioButtonList>". You're writing out HTML and can only write out HTML in either case.

    I'd suggest placing a repeater on the page, binding the nodelist to the repeater and then handling the repeater's DataBound event, in which you take items from the nodelist and assign values to various items in your Repeater's ItemTemplate.

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