473,473 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# XPath - get data without using XPathNodeIterator? Help!

Hi there,

I had this xml file with me (not yet consider implementing xml namespaces yet).

<?xml version='1.0'?>
<Object>
<Windows>
<EID>1</EID>
<EDesc>Error 1</EDesc>
</Windows>
<Windows>
<EID>2</EID>
<EDesc>Error 2</EDesc>
</Windows>
</Object>

My C# code:

private void button1_Click(object sender, System.EventArgs e)
{
try
{
XPathDocument xmldoc = new XPathDocument("IrisException.xml");

XPathNavigator nav = xmldoc.CreateNavigator();

XPathNodeIterator iterator;

try
{
iterator = nav.Select("//EID[. ='1']//parent::node()/EDesc");
}
catch(XPathException XPathExp)
{
Debug.WriteLine(XPathExp.Message);
return;
}

while (iterator.MoveNext())
{
Debug.WriteLine(iterator.Current.Value);
}
}
catch(Exception OtherExp)
{
Debug.WriteLine(OtherExp.Message);
}
}

I really don't want to use iterator. In the xml file, there is not extra occurence of a keyword.

Example,

1,2, 3,, 4, 5 - no occurence of same EID

1, 1, 3, 2, 2 - won't exist this in the xml file

How am i suppose to query using xpath 1 record only?

Please help! Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
6 15475
Hi again,

I manage to do that with XmlDocument. But i would prefer using XPathDocument. Any helps?

This code below is working:

XmlDocument xmldoc = new XmlDocument();

xmldoc.Load("IrisException.xml");

XmlNode irisNode = xmldoc.SelectSingleNode("//EID[. ='1']//parent::node()/EDesc");

Debug.WriteLine(iNode.InnerText.ToString());

Any help please? Thanks.
--
Regards,
Chua Wen Ching :)
"Chua Wen Ching" wrote:
Hi there,

I had this xml file with me (not yet consider implementing xml namespaces yet).

<?xml version='1.0'?>
<Object>
<Windows>
<EID>1</EID>
<EDesc>Error 1</EDesc>
</Windows>
<Windows>
<EID>2</EID>
<EDesc>Error 2</EDesc>
</Windows>
</Object>

My C# code:

private void button1_Click(object sender, System.EventArgs e)
{
try
{
XPathDocument xmldoc = new XPathDocument("IrisException.xml");

XPathNavigator nav = xmldoc.CreateNavigator();

XPathNodeIterator iterator;

try
{
iterator = nav.Select("//EID[. ='1']//parent::node()/EDesc");
}
catch(XPathException XPathExp)
{
Debug.WriteLine(XPathExp.Message);
return;
}

while (iterator.MoveNext())
{
Debug.WriteLine(iterator.Current.Value);
}
}
catch(Exception OtherExp)
{
Debug.WriteLine(OtherExp.Message);
}
}

I really don't want to use iterator. In the xml file, there is not extra occurence of a keyword.

Example,

1,2, 3,, 4, 5 - no occurence of same EID

1, 1, 3, 2, 2 - won't exist this in the xml file

How am i suppose to query using xpath 1 record only?

Please help! Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2


Chua Wen Ching wrote:

I manage to do that with XmlDocument. But i would prefer using XPathDocument. Any helps?

This code below is working:

XmlDocument xmldoc = new XmlDocument();

xmldoc.Load("IrisException.xml");

XmlNode irisNode = xmldoc.SelectSingleNode("//EID[. ='1']//parent::node()/EDesc");

Debug.WriteLine(iNode.InnerText.ToString());


There is no SelectSingleNode with XPathDocument so all you can do is to
use SelectNode and then read out the value, in the case of your example
document you could use

using System;
using System.Xml;
using System.Xml.XPath;

public class Test20040710 {
public static void Main (string[] args) {
XPathDocument xpathDocument = new
XPathDocument(@"test20040710.xml", XmlSpace.Preserve);
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XPathExpression xpathExpression =
xpathNavigator.Compile("//*[EID = '1']/EDesc/text()");
XPathNodeIterator xpathIterator =
xpathNavigator.Select(xpathExpression);
if (xpathIterator.MoveNext()) {
Console.WriteLine("Found text: {0}.", xpathIterator.Current.Value);
}
else {
Console.WriteLine("Not found.");
}
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #3
Thanks Martin Honnen, it works great. But i had still some doubts, hope you can help me out.

1) With your method and my xmldocument way, which one is faster? Does it makes any difference to use xpathdocument over xmldocument?

2) My xml file, does not contains namespaces and schemas. If i use or don't use them, does it makes any difference?

3) Your expression:

you: //*[EID = '1']/EDesc/text()

mine: //EID[. ='1']//parent::node()/EDesc

It seems to be lots of difference. Can my expression to be used with your way?

4) What if my xml file is like this, how will the expression looks like? I am trying to get the right attribute and the right eid with the right edesc. If not, can namespaces or schemas help me to solve.

<?xml version='1.0'?>
<Object>
<Windows id="WindowsXP">
<EID>1</EID>
<EDesc>Error 1</EDesc>
</Windows>
<Windows id="Windows98">
<EID>2</EID>
<EDesc>Error 2</EDesc>
</Windows>
</Object>

Anyway thanks for the help. Really appreciate that :)
--
Regards,
Chua Wen Ching :)
"Martin Honnen" wrote:


Chua Wen Ching wrote:

I manage to do that with XmlDocument. But i would prefer using XPathDocument. Any helps?

This code below is working:

XmlDocument xmldoc = new XmlDocument();

xmldoc.Load("IrisException.xml");

XmlNode irisNode = xmldoc.SelectSingleNode("//EID[. ='1']//parent::node()/EDesc");

Debug.WriteLine(iNode.InnerText.ToString());


There is no SelectSingleNode with XPathDocument so all you can do is to
use SelectNode and then read out the value, in the case of your example
document you could use

using System;
using System.Xml;
using System.Xml.XPath;

public class Test20040710 {
public static void Main (string[] args) {
XPathDocument xpathDocument = new
XPathDocument(@"test20040710.xml", XmlSpace.Preserve);
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XPathExpression xpathExpression =
xpathNavigator.Compile("//*[EID = '1']/EDesc/text()");
XPathNodeIterator xpathIterator =
xpathNavigator.Select(xpathExpression);
if (xpathIterator.MoveNext()) {
Console.WriteLine("Found text: {0}.", xpathIterator.Current.Value);
}
else {
Console.WriteLine("Not found.");
}
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #4


Chua Wen Ching wrote:

1) With your method and my xmldocument way, which one is faster? Does
it makes any difference to use xpathdocument over xmldocument?
XPathDocument is faster as it is a readonly document tuned to allow
searches while XmlDocument is slower while allowing you not only to
search for nodes but also to change them.
2) My xml file, does not contains namespaces and schemas. If i use or
don't use them, does it makes any difference?
Namespaces are possible, here is an example taken directly from the SDK
docs:

XPathDocument doc = new XPathDocument("booksort.xml");
XPathNavigator nav = doc.CreateNavigator();

//Select all ISBN attributes.
XPathExpression expr;
expr = nav.Compile("/bookstore/book/@bk:ISBN");

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");
expr.SetContext(nsmgr);

//Display the selection.
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext()){
Console.WriteLine(iterator.Current.ToString());
}

Schema doesn't really relate to XPath, of course you can validate your
XML against a schema before you load it into an XPathDocument.
3) Your expression:

you: //*[EID = '1']/EDesc/text()

mine: //EID[. ='1']//parent::node()/EDesc

It seems to be lots of difference. Can my expression to be used with
your way?
I had a look at your document and it seemed to me you wanted to read out
the text inside of a <EDesc> element, that is why I have appended text()
to the XPath expression. That is necessary if you want to read out the
Value property directly. The restructuring is just a matter of taste
meaning you could use
//EID[. ='1']//parent::node()/EDesc/text()
as well.
4) What if my xml file is like this, how will the expression looks
like? I am trying to get the right attribute and the right eid with
the right edesc. If not, can namespaces or schemas help me to solve.
<?xml version='1.0'?>
<Object>
<Windows id="WindowsXP">
<EID>1</EID>
<EDesc>Error 1</EDesc>
</Windows>
<Windows id="Windows98">
<EID>2</EID>
<EDesc>Error 2</EDesc>
</Windows>
</Object>


You do not need namespaces or schemas to search any XML document with
XPath, how the XPath expression has to look depends on what you want to
look for, guessing from your previous example you still want
//EID[. ='1']//parent::node()/EDesc/text()
If you want to use XPath and are not feeling strong enough then maybe
look into some tutorials, for instance
http://www.w3schools.com/xpath/default.asp

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #5
Thanks Martin Honnen for the great explanation.

Thanks Desi, but i am looking forward on XPath, so next time when xquery is official out, i can migrate it easier.. query query query :)

Thanks again.
--
Regards,
Chua Wen Ching :)
"Desi Cortez" wrote:
Martin Honnen wrote:


Chua Wen Ching wrote:

1) With your method and my xmldocument way, which one is faster? Does
it makes any difference to use xpathdocument over xmldocument?


XPathDocument is faster as it is a readonly document tuned to allow
searches while XmlDocument is slower while allowing you not only to
search for nodes but also to change them.


There is also XmlReader

Nov 16 '05 #6
Hi Jermaine Franklin,

Thanks for the url.

I am thinking of writing the whole new xquery library by myself once i got approval from my boss to contrinue developing
with xquery. But first let me read more about xpath, xml schemas.. haha!

Cheers.
--
Regards,
Chua Wen Ching :)
"Jermaine Franklin" wrote:
Chua Wen Ching wrote:
Thanks Martin Honnen for the great explanation.

Thanks Desi, but i am looking forward on XPath, so next time when xquery
is official out, i can migrate it easier.. query query query :)

Thanks again.


Yes, I hear you.

Thanks for mentioning XQuery -- I had not heard of it before your posts and
last night started reading up on it.

I am not sure how XQuery integrates with c# since it seems to imply that it
is its own parser. I guess there will be some built in XQuery assembly
in .NET.

Here is some sample chapters from a cool book I found on Amazon about XQuery
-- the best part is where it contrasts XML versus RDBMS:

http://www.amazon.com/gp/reader/0321...01#reader-link

Nov 16 '05 #7

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

Similar topics

4
by: Helmut Dirtinger | last post by:
Hi Compontents of an xml file are mapped to the different node types of the xpath data model. An element is mapped to an element node, an attribute node represents an attribute and its value...
2
by: Samuel R. Neff | last post by:
What options are available for doing full-text searches of database data without using a database-specific full-text engine? The only option I've found is Google's Search Appliance but it's an...
3
by: mamatha | last post by:
Hi I have one VB application,in that one form consits of textbox. Textbox conatains some data,i want to copy and paste that data into another application without using clipboard oprations.How...
5
by: Ben Fidge | last post by:
Hi, I've had an ASP project dumped on me written in VBScript. I'm actually a C#/ASP.NET developer and am struggling trying to find a way to sort the result of a XPath query executed using...
11
by: ulyses | last post by:
Let's assume I have following file: 2938929384902491233..... 923949919199191919112.... File contains INTs only. What is more they are huge. For example first row in file may contain integer...
5
by: lbolognini | last post by:
Hi all, I was wondering if there's a technique to use POST instead of GET to send data back to a form so to refill it with user input after this hasn't passed validation. Any clues? ...
2
by: Bernard Dhooghe | last post by:
The information center writes: "Encryption Algorithm: The internal encryption algorithm used is RC2 block cipher with padding, the 128-bit secret key is derived from the password using a MD2...
6
by: Kindler Chase | last post by:
I'm trying to iterate through a set of nodes and then edit/delete specific attributes using XPathNodeIterator. Adding attributes is no problem. My first question is how do I delete an attribute...
3
by: priyamtheone | last post by:
Is it possible to add, edit, view and delete records using a file only (instead of a database) and .Net 2005 (VB.Net/C#)? The concept is that a file that'll work more-or-less like a database. One...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.