How to insert and query nodes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SmFzb25UODA=?=

    #1

    How to insert and query nodes?

    Hi all,

    I need to use XML to store some log data and then query that data. I'm
    struggling with the right terminology, which is hindering my ability to
    successfully search for a solution. Thanks in advance for your help!

    Two questions:
    1. In VB.NET, how can I append new "Log" record to a "Site" record?
    2. How can I query all of "Log" records in a single "Site" for an average
    "ResponseTi me"?

    ------------------------------

    <?xml version="1.0" standalone="yes " ?>
    <Sites>
    <Site Host="www.mysit e1.com" IP="1.1.1.1" />
    <Site Host="www.mysit e2.com" IP="1.1.1.2" />
    <Site Host="www.mysit e3.com" IP="1.1.1.3">
    <Log Time="1/1/2008 12:00:00 AM" Status="Success " ResponseTime="1 0" />
    <Log Time="1/1/2008 12:00:01 AM" Status="Failure " ResponseTime="1 0" />
    </Site>
    <Site Host="www.mysit e4.com" IP="1.1.1.4">
    <Log Time="1/1/2008 12:00:00 AM" Status="Success " ResponseTime="1 0" />
    <Log Time="1/1/2008 12:00:01 AM" Status="Failure " ResponseTime="1 0" />
    </Site>
    </Sites>

  • Martin Honnen

    #2
    Re: How to insert and query nodes?

    JasonT80 wrote:
    Two questions:
    1. In VB.NET, how can I append new "Log" record to a "Site" record?
    With .NET 1.x, 2.0 and 3.0 you would use System.Xml.XmlD ocument e.g.
    Dim doc As New XmlDocument()
    doc.Load("file. xml")
    Dim log As XmlElement = doc.CreateEleme nt("Log")
    log.SetAttribut e("Time", "...")
    log.SetAttribut e("Status", "...")
    doc.SelectSingl eNode("Sites/Site[@Host =
    'www.mysite1.co m']").AppendChild( log)
    doc.Save("file. xml")

    With .NET 3.5 you would use LINQ to XML. Let us know whether you use
    that version and need sample code.
    2. How can I query all of "Log" records in a single "Site" for an average
    "ResponseTi me"?
    A solutuon again depends on whether you can use LINQ to XML so please
    let us know which version of the .NET framework you use/want to use.

    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

    Comment

    • =?Utf-8?B?SmFzb25UODA=?=

      #3
      Re: How to insert and query nodes?

      The environment has only .NET 2.0. Thanks for the help!

      --
      Jasont80


      "Martin Honnen" wrote:
      JasonT80 wrote:
      >
      Two questions:
      1. In VB.NET, how can I append new "Log" record to a "Site" record?
      >
      With .NET 1.x, 2.0 and 3.0 you would use System.Xml.XmlD ocument e.g.
      Dim doc As New XmlDocument()
      doc.Load("file. xml")
      Dim log As XmlElement = doc.CreateEleme nt("Log")
      log.SetAttribut e("Time", "...")
      log.SetAttribut e("Status", "...")
      doc.SelectSingl eNode("Sites/Site[@Host =
      'www.mysite1.co m']").AppendChild( log)
      doc.Save("file. xml")
      >
      With .NET 3.5 you would use LINQ to XML. Let us know whether you use
      that version and need sample code.
      >
      2. How can I query all of "Log" records in a single "Site" for an average
      "ResponseTi me"?
      >
      A solutuon again depends on whether you can use LINQ to XML so please
      let us know which version of the .NET framework you use/want to use.
      >
      --
      >
      Martin Honnen --- MVP XML
      http://JavaScript.FAQTs.com/
      >

      Comment

      • Martin Honnen

        #4
        Re: How to insert and query nodes?

        JasonT80 wrote:
        2. How can I query all of "Log" records in a single "Site" for an average
        "ResponseTi me"?
        >
        ------------------------------
        >
        <?xml version="1.0" standalone="yes " ?>
        <Sites>
        <Site Host="www.mysit e1.com" IP="1.1.1.1" />
        <Site Host="www.mysit e2.com" IP="1.1.1.2" />
        <Site Host="www.mysit e3.com" IP="1.1.1.3">
        <Log Time="1/1/2008 12:00:00 AM" Status="Success " ResponseTime="1 0" />
        <Log Time="1/1/2008 12:00:01 AM" Status="Failure " ResponseTime="1 0" />
        </Site>
        <Site Host="www.mysit e4.com" IP="1.1.1.4">
        <Log Time="1/1/2008 12:00:00 AM" Status="Success " ResponseTime="1 0" />
        <Log Time="1/1/2008 12:00:01 AM" Status="Failure " ResponseTime="1 0" />
        </Site>
        </Sites>
        Here is an example that computes the average ResponseTime for those Site
        elements that have at least on Log child element with a ResponseTime
        attribute:

        Imports System
        Imports System.Xml.XPat h


        Module Module1

        Sub Main()
        Dim doc As New XPathDocument(" ..\..\XMLFile1. xml")
        For Each site As XPathNavigator In
        doc.CreateNavig ator().Select(" Sites/Site[Log/@ResponseTime]")
        ComputeAverage( site)
        Next
        End Sub

        Sub ComputeAverage( ByVal site As XPathNavigator)
        Console.WriteLi ne(site.Evaluat e("sum(Log/@ResponseTime) div
        count(Log/@ResponseTime)" ))
        End Sub

        End Module


        So with XPath the solution is to evaluate the XPath expression
        sum(Log/@ResponseTime) div count(Log/@ResponseTime)
        with the Site element as the context node.



        --

        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/

        Comment

        • =?Utf-8?B?SmFzb25UODA=?=

          #5
          Re: How to insert and query nodes?

          That worked. THANKS!

          --
          Jasont80


          "Martin Honnen" wrote:
          JasonT80 wrote:
          >
          2. How can I query all of "Log" records in a single "Site" for an average
          "ResponseTi me"?

          ------------------------------

          <?xml version="1.0" standalone="yes " ?>
          <Sites>
          <Site Host="www.mysit e1.com" IP="1.1.1.1" />
          <Site Host="www.mysit e2.com" IP="1.1.1.2" />
          <Site Host="www.mysit e3.com" IP="1.1.1.3">
          <Log Time="1/1/2008 12:00:00 AM" Status="Success " ResponseTime="1 0" />
          <Log Time="1/1/2008 12:00:01 AM" Status="Failure " ResponseTime="1 0" />
          </Site>
          <Site Host="www.mysit e4.com" IP="1.1.1.4">
          <Log Time="1/1/2008 12:00:00 AM" Status="Success " ResponseTime="1 0" />
          <Log Time="1/1/2008 12:00:01 AM" Status="Failure " ResponseTime="1 0" />
          </Site>
          </Sites>
          >
          Here is an example that computes the average ResponseTime for those Site
          elements that have at least on Log child element with a ResponseTime
          attribute:
          >
          Imports System
          Imports System.Xml.XPat h
          >
          >
          Module Module1
          >
          Sub Main()
          Dim doc As New XPathDocument(" ..\..\XMLFile1. xml")
          For Each site As XPathNavigator In
          doc.CreateNavig ator().Select(" Sites/Site[Log/@ResponseTime]")
          ComputeAverage( site)
          Next
          End Sub
          >
          Sub ComputeAverage( ByVal site As XPathNavigator)
          Console.WriteLi ne(site.Evaluat e("sum(Log/@ResponseTime) div
          count(Log/@ResponseTime)" ))
          End Sub
          >
          End Module
          >
          >
          So with XPath the solution is to evaluate the XPath expression
          sum(Log/@ResponseTime) div count(Log/@ResponseTime)
          with the Site element as the context node.
          >
          >
          >
          --
          >
          Martin Honnen --- MVP XML
          http://JavaScript.FAQTs.com/
          >

          Comment

          Working...