Connecting Tech Pros Worldwide Help | Site Map

How to insert and query nodes?

  #1  
Old October 7th, 2008, 01:45 PM
=?Utf-8?B?SmFzb25UODA=?=
Guest
 
Posts: n/a
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
"ResponseTime"?

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

<?xml version="1.0" standalone="yes" ?>
<Sites>
<Site Host="www.mysite1.com" IP="1.1.1.1" />
<Site Host="www.mysite2.com" IP="1.1.1.2" />
<Site Host="www.mysite3.com" IP="1.1.1.3">
<Log Time="1/1/2008 12:00:00 AM" Status="Success" ResponseTime="10" />
<Log Time="1/1/2008 12:00:01 AM" Status="Failure" ResponseTime="10" />
</Site>
<Site Host="www.mysite4.com" IP="1.1.1.4">
<Log Time="1/1/2008 12:00:00 AM" Status="Success" ResponseTime="10" />
<Log Time="1/1/2008 12:00:01 AM" Status="Failure" ResponseTime="10" />
</Site>
</Sites>

  #2  
Old October 7th, 2008, 02:05 PM
Martin Honnen
Guest
 
Posts: n/a

re: How to insert and query nodes?


JasonT80 wrote:
Quote:
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.XmlDocument e.g.
Dim doc As New XmlDocument()
doc.Load("file.xml")
Dim log As XmlElement = doc.CreateElement("Log")
log.SetAttribute("Time", "...")
log.SetAttribute("Status", "...")
doc.SelectSingleNode("Sites/Site[@Host =
'www.mysite1.com']").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.
Quote:
2. How can I query all of "Log" records in a single "Site" for an average
"ResponseTime"?
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/
  #3  
Old October 7th, 2008, 08:25 PM
=?Utf-8?B?SmFzb25UODA=?=
Guest
 
Posts: n/a

re: How to insert and query nodes?


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

--
Jasont80


"Martin Honnen" wrote:
Quote:
JasonT80 wrote:
>
Quote:
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.XmlDocument e.g.
Dim doc As New XmlDocument()
doc.Load("file.xml")
Dim log As XmlElement = doc.CreateElement("Log")
log.SetAttribute("Time", "...")
log.SetAttribute("Status", "...")
doc.SelectSingleNode("Sites/Site[@Host =
'www.mysite1.com']").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.
>
Quote:
2. How can I query all of "Log" records in a single "Site" for an average
"ResponseTime"?
>
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/
>
  #4  
Old October 8th, 2008, 01:05 PM
Martin Honnen
Guest
 
Posts: n/a

re: How to insert and query nodes?


JasonT80 wrote:
Quote:
2. How can I query all of "Log" records in a single "Site" for an average
"ResponseTime"?
>
------------------------------
>
<?xml version="1.0" standalone="yes" ?>
<Sites>
<Site Host="www.mysite1.com" IP="1.1.1.1" />
<Site Host="www.mysite2.com" IP="1.1.1.2" />
<Site Host="www.mysite3.com" IP="1.1.1.3">
<Log Time="1/1/2008 12:00:00 AM" Status="Success" ResponseTime="10" />
<Log Time="1/1/2008 12:00:01 AM" Status="Failure" ResponseTime="10" />
</Site>
<Site Host="www.mysite4.com" IP="1.1.1.4">
<Log Time="1/1/2008 12:00:00 AM" Status="Success" ResponseTime="10" />
<Log Time="1/1/2008 12:00:01 AM" Status="Failure" ResponseTime="10" />
</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.XPath


Module Module1

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

Sub ComputeAverage(ByVal site As XPathNavigator)
Console.WriteLine(site.Evaluate("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/
  #5  
Old October 14th, 2008, 12:45 PM
=?Utf-8?B?SmFzb25UODA=?=
Guest
 
Posts: n/a

re: How to insert and query nodes?


That worked. THANKS!

--
Jasont80


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

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

<?xml version="1.0" standalone="yes" ?>
<Sites>
<Site Host="www.mysite1.com" IP="1.1.1.1" />
<Site Host="www.mysite2.com" IP="1.1.1.2" />
<Site Host="www.mysite3.com" IP="1.1.1.3">
<Log Time="1/1/2008 12:00:00 AM" Status="Success" ResponseTime="10" />
<Log Time="1/1/2008 12:00:01 AM" Status="Failure" ResponseTime="10" />
</Site>
<Site Host="www.mysite4.com" IP="1.1.1.4">
<Log Time="1/1/2008 12:00:00 AM" Status="Success" ResponseTime="10" />
<Log Time="1/1/2008 12:00:01 AM" Status="Failure" ResponseTime="10" />
</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.XPath
>
>
Module Module1
>
Sub Main()
Dim doc As New XPathDocument("..\..\XMLFile1.xml")
For Each site As XPathNavigator In
doc.CreateNavigator().Select("Sites/Site[Log/@ResponseTime]")
ComputeAverage(site)
Next
End Sub
>
Sub ComputeAverage(ByVal site As XPathNavigator)
Console.WriteLine(site.Evaluate("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/
>
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM