473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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
"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>

Oct 7 '08 #1
4 1616
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.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.
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/
Oct 7 '08 #2
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.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.
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/
Oct 7 '08 #3
JasonT80 wrote:
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/
Oct 8 '08 #4
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
"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/
Oct 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Gordon | last post by:
Hi Folks, I wonder if anyone can help me with the following (perhaps trivial) problem: <xml> <node name="a" type="a"/> <node name="b" type=""/> <node name="c"/> <node name="d" type="b"/>...
4
by: Jim | last post by:
Hi I'm looking to take an existing XML document, query for certain nodes, and 'recreate' the document with just the relevant nodes. I'm currently using XPath - I have established the pattern that...
8
by: Jack | last post by:
I have a test database that I have built in a 3 partition (and 3 node) environment. I have defined all the tables so they have the same partition key. The tables (7 of them) form a hierarchical...
2
by: CSN | last post by:
I have a table with these columns: id, node, parent_node_id The top-most nodes would have a parent_node_id of NULL. Is it possible to get a node, and all its parent nodes, in a single query?...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
3
by: Markus | last post by:
Hi! I wanted to select a subset of nodes (list = selectNodes("parent/child") from a XmlDocument, then remove all (parentNode.removeAll();) child-nodes and insert the previous selected nodes...
3
by: 0to60 | last post by:
Please help! I'm using the following code to get an XML doc: string str = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison"; System.Net.HttpWebRequest request =...
5
by: tschulken | last post by:
I have a query where i need to look for a value of a lower level xml element based on the value of a parent element existing first. Here is a simple example of the xml <S3Client> <Buttons>...
1
by: newasp | last post by:
Hi I was wondering if we can use Xpath query for finding nodes that ends with a 'string' value entered by the user. For example we can use the following query for finding the nodes that starts...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.