Results 1 to 4 of 4

Thread: XML Parsing (Not From File)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Arrow XML Parsing (Not From File)

    Hope you are well.

    Just a little problem; seemingly easy but doesn't seem quite so straightforward as it should be. I am pulling an XML file from a server using the web browser control and the using the DOM innertext property to assign the contents of that XML page to a variable.

    I would like to be able to efficiently parse through this variable as if it were an XML file. I have posted the output below.

    I can work out how to load an XML file using the LoadXML method from a disc or URL but I would prefer to parse it from within the variable. I know this should be simple but I was wondering what the best class to use would be as the most efficient / easy way of parsing data.

    I am basically looking to extract each of the details for each <application> into an array.

    Code:
    - <response>
    - <result>
      <application_owner>mdcpinpoint</application_owner> 
    - <application_list>
    - <applications>
    - <application>
      <application_name>Activity Log</application_name> 
      <link_name>activity-log</link_name> 
      <access>private</access> 
      <created_time>2011-12-01 07:33:35.0</created_time> 
      </application>
    - <application>
      <application_name>Contact</application_name> 
      <link_name>contact</link_name> 
      <access>private</access> 
      <created_time>2011-12-01 07:32:53.0</created_time> 
      </application>
    - <application>
      <application_name>Visicode New Business</application_name> 
      <link_name>visicode-new-business</link_name> 
      <access>private</access> 
      <created_time>2011-11-24 03:54:19.0</created_time> 
      </application>
      </applications>
      </application_list>
      </result>
      </response>:)

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: XML Parsing (Not From File)

    Consider SomeStringVar as the string you are working with innertext data.

    Note I removed the node dashes from your data provided.

    Using XDocument.Parse method you can query the data as shown.

    Code:
    Private Sub Button2_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles Button2.Click
    
       Dim SomeStringVar As String = _
          <response>
             <result>
                <application_owner>mdcpinpoint</application_owner>
                <application_list>
                   <applications>
                      <application>
                         <application_name>Activity Log</application_name>
                         <link_name>activity-log</link_name>
                         <access>private</access>
                         <created_time>2011-12-01 07:33:35.0</created_time>
                      </application>
                      <application>
                         <application_name>Contact</application_name>
                         <link_name>contact</link_name>
                         <access>private</access>
                         <created_time>2011-12-01 07:32:53.0</created_time>
                      </application>
                      <application>
                         <application_name>Visicode New Business</application_name>
                         <link_name>visicode-new-business</link_name>
                         <access>private</access>
                         <created_time>2011-11-24 03:54:19.0</created_time>
                      </application>
                   </applications>
                </application_list>
             </result>
          </response>.ToString
    
       Dim Doc As New XDocument
       Doc = XDocument.Parse(SomeStringVar)
    
       Dim Test = (From T In Doc...<application> _
                   Select Name = T.<application_name>.Value, _
                          Created = T.<created_time>.Value).ToList
    
       For Each app In Test
          Console.WriteLine("{0} {1}", app.Created, app.Name)
       Next
    
    End Sub
    By creating a Schema you can get context help (Intellisense) when writing a LINQ statement

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema 
       attributeFormDefault="unqualified" 
       elementFormDefault="qualified" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="response">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="result">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="application_owner" type="xs:string" />
                  <xs:element name="application_list">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="applications">
                          <xs:complexType>
                            <xs:sequence>
                              <xs:element maxOccurs="unbounded" name="application">
                                <xs:complexType>
                                  <xs:sequence>
                                    <xs:element name="application_name" type="xs:string" />
                                    <xs:element name="link_name" type="xs:string" />
                                    <xs:element name="access" type="xs:string" />
                                    <xs:element name="created_time" type="xs:string" />
                                  </xs:sequence>
                                </xs:complexType>
                              </xs:element>
                            </xs:sequence>
                          </xs:complexType>
                        </xs:element>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Re: XML Parsing (Not From File)

    Kevin, many many thanks!

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: XML Parsing (Not From File)

    Quote Originally Posted by intraman View Post
    Kevin, many many thanks!
    Your welcome.

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