HighTechTalks DotNet Forums  

Newbie question

Dotnet XML microsoft.public.dotnet.xml


Discuss Newbie question in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Phil Hunt
 
Posts: n/a

Default Newbie question - 12-04-2007 , 06:43 PM






I have this XML

<?xml version="1.0" encoding="utf-8"?>
<PORMessage key="new" j:effectiveDate="2007-05-21"
xmlns:j="http://www.xxx.com/xxxx/3.0.3"
xmlns="http://www.xxx.com">
<ActivityID>
<ID>12345</ID>
</ActivityID>
</PORMessage>

=================== END =====

Then my C# code is:

xmlDoc.LoadXml(xmlMsg);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("j", "http://www.xxx.com/xxxx/3.0.3");

XmlNode porNode = xmlDoc.SelectSingleNode("//PORMessage", nsmgr);

Question: porNode always return null. I have some similar program that
works. I don't know why this one does not. If I take out the 3rd line on the
xml, it return the right node.

How do I make it work and an explanation would be appreciated. Thanks



Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Newbie question - 12-05-2007 , 08:10 AM






Phil Hunt wrote:
Quote:
I have this XML

?xml version="1.0" encoding="utf-8"?
PORMessage key="new" j:effectiveDate="2007-05-21"
xmlns:j="http://www.xxx.com/xxxx/3.0.3"
xmlns="http://www.xxx.com"
ActivityID
ID>12345</ID
/ActivityID
/PORMessage

=================== END =====

Then my C# code is:

xmlDoc.LoadXml(xmlMsg);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("j", "http://www.xxx.com/xxxx/3.0.3");
nsmgr.AddNamespace("df", "http://www.xxx.com/");

Quote:
XmlNode porNode = xmlDoc.SelectSingleNode("//PORMessage", nsmgr);
XmlNode porNode = xmlDoc.SelectSingleNode("//df:PORMessage", nsmgr);

The default namespace declaration (xmlns="http://www.xxx.com/") means
that the elements are in the namespace http://www.xxx.com/. With XPath
1.0 to select elements in a namespace you need to bind a prefix (e.g.
df) to the namespace URI and use the prefix to qualify element names in
your XPath expression.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Phil H
 
Posts: n/a

Default Re: Newbie question - 12-06-2007 , 08:29 AM



thanks. that's it.


"Martin Honnen" <mahotrash (AT) yahoo (DOT) de> wrote

Quote:
Phil Hunt wrote:
I have this XML

?xml version="1.0" encoding="utf-8"?
PORMessage key="new" j:effectiveDate="2007-05-21"
xmlns:j="http://www.xxx.com/xxxx/3.0.3"
xmlns="http://www.xxx.com"
ActivityID
ID>12345</ID
/ActivityID
/PORMessage

=================== END =====

Then my C# code is:

xmlDoc.LoadXml(xmlMsg);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("j", "http://www.xxx.com/xxxx/3.0.3");

nsmgr.AddNamespace("df", "http://www.xxx.com/");

XmlNode porNode = xmlDoc.SelectSingleNode("//PORMessage", nsmgr);

XmlNode porNode = xmlDoc.SelectSingleNode("//df:PORMessage", nsmgr);

The default namespace declaration (xmlns="http://www.xxx.com/") means that
the elements are in the namespace http://www.xxx.com/. With XPath 1.0 to
select elements in a namespace you need to bind a prefix (e.g. df) to the
namespace URI and use the prefix to qualify element names in your XPath
expression.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.