Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 28th, 2006, 01:02 AM
Registered User
 
Join Date: Aug 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gijomk Send a message via Yahoo to gijomk
Default C# XML--SelectSingleNode/SelectNodes is failing.

I have one xml (which is HL7 format) as follows

----------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<PORP_IN000001 xmlns="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3
PORP_IN000001.xsd" ITSVersion="XML_1.0">
    <id/>
    <creationTime/>
    <interactionId/>
    <processingCode/>
    <processingModeCode/>
    <acceptAckCode/>
    <receiver>
        <device>
            <id/>
        </device>
    </receiver>
    <sender>
        <device>
            <id/>
        </device>
    </sender>
    <controlActProcess moodCode="EVN">
        <subject>
                 etc.................................
.......................
......
    </controlActProcess>
</PORP_IN000001>
----------------------------------

Please note that the above xml file is using the schema file "PORP_IN000001.xsd" and this file interally calls other schema files. There are 15 schema files using by this xml file. Only one schema file is directly refered in the xml file. All other schema files are called from the main schema file "PORP_IN000001.xsd"

I am trying to get the node from the xml file using c# code as follows

XmlDocument objXml=new XmlDocument();
objXml.Load(reader);
oNsMgr = new XmlNamespaceManager(objXml.NameTable);
oNsMgr.AddNamespace("", "urn:hl7-org:v3");
oNsMgr.AddNamespace("mif", "urn:hl7-org:v3/mif");
oNsMgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlNode Node=objXml.SelectSingleNode("//receiver");

But here objXml.SelectSingleNode("//receiver") is returning null value. Actully i have one node already present in the xml file.
Why the above query is failing. Can anybody help me?


Regards,

Gijo



 
Old August 28th, 2006, 03:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This is becoming the XSLT most FA of FAQs. See http://www.dpawson.co.uk/xsl/sect2/N5536.html especially numbers 13 and 23. You need to associate a prefix with the HL7 namespace and use that in your XPath statements.
Code:
oNsMgr.AddNamespace("hl7", "urn:hl7-org:v3");
XmlNode Node=objXml.SelectSingleNode("//hl7:receiver");
//or preferably
XmlNode Node=objXml.SelectSingleNode("/*/hl7:receiver");
It is nothing to do with the schema, just the way the namespaces are declared.

--

Joe (Microsoft MVP - XML)
 
Old August 28th, 2006, 03:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XPath 1.0 an unprefixed name always refers to a name that is in no namespace. To refer to a name in a namespace, you need to bind an explicit prefix.

I'm a little surprised that

oNsMgr.AddNamespace("", "urn:hl7-org:v3")

is allowed, since I don't see what meaning it can have.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectSingleNode throwing exception: anup_daware .NET Framework 2.0 1 February 3rd, 2008 12:04 PM
SelectNodes, SelectSingleNode weirdness dantell BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 October 19th, 2006 12:39 PM
C# XML--SelectSingleNode/SelectNodes is failing. gijomk C# 1 August 29th, 2006 09:48 AM
XML Namespace and ASP SelectNodes werD420 Classic ASP XML 1 August 13th, 2005 04:41 AM
MSXML - SelectNodes and XPath billy_bob_the_3rd Beginning VB 6 2 March 10th, 2005 06:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.