473,836 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# XPath - get data without using XPathNodeIterat or? 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(o bject sender, System.EventArg s e)
{
try
{
XPathDocument xmldoc = new XPathDocument(" IrisException.x ml");

XPathNavigator nav = xmldoc.CreateNa vigator();

XPathNodeIterat or iterator;

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

while (iterator.MoveN ext())
{
Debug.WriteLine (iterator.Curre nt.Value);
}
}
catch(Exception OtherExp)
{
Debug.WriteLine (OtherExp.Messa ge);
}
}

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 15515
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("Ir isException.xml ");

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

Debug.WriteLine (iNode.InnerTex t.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(o bject sender, System.EventArg s e)
{
try
{
XPathDocument xmldoc = new XPathDocument(" IrisException.x ml");

XPathNavigator nav = xmldoc.CreateNa vigator();

XPathNodeIterat or iterator;

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

while (iterator.MoveN ext())
{
Debug.WriteLine (iterator.Curre nt.Value);
}
}
catch(Exception OtherExp)
{
Debug.WriteLine (OtherExp.Messa ge);
}
}

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("Ir isException.xml ");

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

Debug.WriteLine (iNode.InnerTex t.ToString());


There is no SelectSingleNod e 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.XPat h;

public class Test20040710 {
public static void Main (string[] args) {
XPathDocument xpathDocument = new
XPathDocument(@ "test20040710.x ml", XmlSpace.Preser ve);
XPathNavigator xpathNavigator = xpathDocument.C reateNavigator( );
XPathExpression xpathExpression =
xpathNavigator. Compile("//*[EID = '1']/EDesc/text()");
XPathNodeIterat or xpathIterator =
xpathNavigator. Select(xpathExp ression);
if (xpathIterator. MoveNext()) {
Console.WriteLi ne("Found text: {0}.", xpathIterator.C urrent.Value);
}
else {
Console.WriteLi ne("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("Ir isException.xml ");

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

Debug.WriteLine (iNode.InnerTex t.ToString());


There is no SelectSingleNod e 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.XPat h;

public class Test20040710 {
public static void Main (string[] args) {
XPathDocument xpathDocument = new
XPathDocument(@ "test20040710.x ml", XmlSpace.Preser ve);
XPathNavigator xpathNavigator = xpathDocument.C reateNavigator( );
XPathExpression xpathExpression =
xpathNavigator. Compile("//*[EID = '1']/EDesc/text()");
XPathNodeIterat or xpathIterator =
xpathNavigator. Select(xpathExp ression);
if (xpathIterator. MoveNext()) {
Console.WriteLi ne("Found text: {0}.", xpathIterator.C urrent.Value);
}
else {
Console.WriteLi ne("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.CreateNavig ator();

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

XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(nav.NameTa ble);
nsmgr.AddNamesp ace("bk", "urn:sample s");
expr.SetContext (nsmgr);

//Display the selection.
XPathNodeIterat or iterator = nav.Select(expr );
while (iterator.MoveN ext()){
Console.WriteLi ne(iterator.Cur rent.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
3601
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 ... and so on. so far so good But what happens to the xml declaration (<?xml version="1.0" ......?>).
2
2200
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 expensive hardware solution and we prefer a software solution. In the past I've used development languages that had OEM Verity support which was wonderful but as far as I can tell the only out-of-the-box search available in ASP.NET is Index...
3
2712
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 can it is possible in VB? If any one knows the source code or any related website URL's please let me know.
5
8503
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 SelectNodes? I'd rather not rely on external XSL files if possible. Any help gratefully received.
11
2517
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 which size is 50MB and the second 30MB. Now we come to my problem. Is there possibility to swap this rows without using system memory (preferably in Unix/Linux)? Is there any function in C to do this?
5
2179
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? Thanks, Lorenzo
2
7699
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 message digest. " and also explains how the length of the encrypted column can be derived.
6
7710
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 using an XPathNodeIterator? Or should I be using something else? In the sample that follows: 1. Grab all "Page" nodes. 2. Loop through all the nodes.
3
1668
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 can add, edit, view, delete and query data to and from the file. The file, either cannot be opened directly or data inside the file should be in some encrypted form so that even if the file is opened no one can comprehend it. Data inside the file can...
0
9812
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9658
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10828
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10579
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5644
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.