473,407 Members | 2,629 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.

Need help with Xpath

Hi.
I have an XML file that looks like this:
<?xml version="1.0" encoding="utf-16"?>
<Transfer>
<Config xmlns="http://www.mysite.com/Transfer/">
<site>NY</site>
</Config>
</Transfer>

I'm trying to get the value of "site", but I'm having some trouble with it.
Here is what I've tried so far:
xmlDoc.DocumentElement.SelectSingleNode("Config/site").InnerXml
xmlDoc.DocumentElement.SelectSingleNode("Config[@*]/site").InnerXml

The attribute xmlns="http://www.mysite.com/Transfer/" might change in the
future so I need be able to use an XPath expression that will work even if
the value changes.

Thanks,
Shawn
Nov 12 '05 #1
3 1398
(1) XPath queries are namespace aware. You must an XmlNamespaceManager in
SelectSingleNode when working with XML documents that use namespaces. For
more information see the online help for XmlNamespaceManager.

(2) xmlns is not a normal attribute, it defines a namespace. If the
namespace changes, it normaly means that the semantics of the XML might have
changed. In your expample <Config> and <site> ar bound to the (default)
namespace "http://www.mysite.com/Transfer/". If this changes to something
other, then you would also need to change your code, where you add the
namespace to the XmlNamespaceManager.

HTH,
Stefan
"Shawn" <bo********@hotmail.com> schrieb im Newsbeitrag
news:e8**************@TK2MSFTNGP10.phx.gbl...
Hi.
I have an XML file that looks like this:
<?xml version="1.0" encoding="utf-16"?>
<Transfer>
<Config xmlns="http://www.mysite.com/Transfer/">
<site>NY</site>
</Config>
</Transfer>

I'm trying to get the value of "site", but I'm having some trouble with
it.
Here is what I've tried so far:
xmlDoc.DocumentElement.SelectSingleNode("Config/site").InnerXml
xmlDoc.DocumentElement.SelectSingleNode("Config[@*]/site").InnerXml

The attribute xmlns="http://www.mysite.com/Transfer/" might change in the
future so I need be able to use an XPath expression that will work even if
the value changes.

Thanks,
Shawn

Nov 12 '05 #2
I've read the documentation on XmlNamespaceManager, but I can't figure out
how to implement in my code. Could you please show me an example?

Shawn

"Stefan Misch" <st**********@removethis.gmx.de> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
(1) XPath queries are namespace aware. You must an XmlNamespaceManager in
SelectSingleNode when working with XML documents that use namespaces. For
more information see the online help for XmlNamespaceManager.

(2) xmlns is not a normal attribute, it defines a namespace. If the
namespace changes, it normaly means that the semantics of the XML might have changed. In your expample <Config> and <site> ar bound to the (default)
namespace "http://www.mysite.com/Transfer/". If this changes to something
other, then you would also need to change your code, where you add the
namespace to the XmlNamespaceManager.

HTH,
Stefan
"Shawn" <bo********@hotmail.com> schrieb im Newsbeitrag
news:e8**************@TK2MSFTNGP10.phx.gbl...
Hi.
I have an XML file that looks like this:
<?xml version="1.0" encoding="utf-16"?>
<Transfer>
<Config xmlns="http://www.mysite.com/Transfer/">
<site>NY</site>
</Config>
</Transfer>

I'm trying to get the value of "site", but I'm having some trouble with
it.
Here is what I've tried so far:
xmlDoc.DocumentElement.SelectSingleNode("Config/site").InnerXml
xmlDoc.DocumentElement.SelectSingleNode("Config[@*]/site").InnerXml

The attribute xmlns="http://www.mysite.com/Transfer/" might change in the future so I need be able to use an XPath expression that will work even if the value changes.

Thanks,
Shawn


Nov 12 '05 #3
See sample below. Note that although no NS prefix is defined on your sample
XML, I define "ns1" as local prefix for the NS. The prefix (or local name)
does not matter, but the NS the prefix stands for. That's why you would need
to modify your program, if the NS would change in the future.

HTH,
Stefan

using System;
using System.Xml;

namespace TestXmlNsMgr
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string sXml =
@"<?xml version='1.0' encoding='utf-16'?>
<Transfer>
<Config xmlns='http://www.mysite.com/Transfer/'>
<site>NY</site>
</Config>
</Transfer>";

XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
XmlNode xmlNode;

try
{
xmlDoc.LoadXml(sXml);
nsMgr.AddNamespace("ns1", "http://www.mysite.com/Transfer/");
xmlNode = xmlDoc.SelectSingleNode("//ns1:site", nsMgr);
Console.WriteLine(xmlNode.InnerXml);
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}

Console.WriteLine("Press [return] to end program...");
Console.ReadLine();
return;
}
}
}
"Shawn" <bo********@hotmail.com> schrieb im Newsbeitrag
news:O5**************@TK2MSFTNGP12.phx.gbl...
I've read the documentation on XmlNamespaceManager, but I can't figure out
how to implement in my code. Could you please show me an example?

Shawn

"Stefan Misch" <st**********@removethis.gmx.de> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
(1) XPath queries are namespace aware. You must an XmlNamespaceManager in
SelectSingleNode when working with XML documents that use namespaces. For
more information see the online help for XmlNamespaceManager.

(2) xmlns is not a normal attribute, it defines a namespace. If the
namespace changes, it normaly means that the semantics of the XML might

have
changed. In your expample <Config> and <site> ar bound to the (default)
namespace "http://www.mysite.com/Transfer/". If this changes to something
other, then you would also need to change your code, where you add the
namespace to the XmlNamespaceManager.

HTH,
Stefan
"Shawn" <bo********@hotmail.com> schrieb im Newsbeitrag
news:e8**************@TK2MSFTNGP10.phx.gbl...
> Hi.
> I have an XML file that looks like this:
> <?xml version="1.0" encoding="utf-16"?>
> <Transfer>
> <Config xmlns="http://www.mysite.com/Transfer/">
> <site>NY</site>
> </Config>
> </Transfer>
>
> I'm trying to get the value of "site", but I'm having some trouble with
> it.
> Here is what I've tried so far:
> xmlDoc.DocumentElement.SelectSingleNode("Config/site").InnerXml
> xmlDoc.DocumentElement.SelectSingleNode("Config[@*]/site").InnerXml
>
> The attribute xmlns="http://www.mysite.com/Transfer/" might change in the > future so I need be able to use an XPath expression that will work even if > the value changes.
>
> Thanks,
> Shawn
>
>



Nov 12 '05 #4

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

Similar topics

4
by: MegaZone | last post by:
I'm having some issues with PHP DOMXML - in particular the get_elements_by_tagname method. Now, the PGP docs on this are, well, sparse, so maybe I'm just doing something stupid. I thought this...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
7
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
2
by: AGB | last post by:
Hi all, I have the following simple XML file. I would like to load all the values in the Name nodes into a drop-down list box: <?xml version="1.0" encoding="utf-8" ?> <OutlineCodeNames>...
3
by: Chua Wen Ching | last post by:
Hi everyone, I am not that good in xml terms and coding in c#. To be honest. ok, just imagine, i had this xml file. <xml> <language>C#</language> <comments>cool language</comments> <desc...
5
by: Chua Wen Ching | last post by:
Hi, I read from this tutorial at codeproject Question A: http://www.codeproject.com/csharp/GsXPathTutorial.asp regarding xpath.. but i try to apply in my situation, and can't get it...
2
by: Joe | last post by:
Hi I am going to receive a dataset downstream and I will not know the the fields number of field relations number of relations So I have to be flexible to take unknown data and be able to...
0
by: Joe | last post by:
Hi Could someone give me a simple explaination of this XSD which is a DataSet with relations The tables are fgFAT,e0,e1,e2,e3 which I have deleted I am trying to make sense of the part below...
4
by: Peter | last post by:
Hi! I am frustrated! I spent a lot of time designing a custom dataset schema (XSD file) using the Dataset designer. In my main form I had created an instance of a typed dataset using that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
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.