?
Solved

Access elements of a node with namespace prefixs  using MSXML (#C)

Posted on 2010-01-12
3
Medium Priority
?
486 Views
Last Modified: 2013-11-11
I am iterating through a nodelist returned from an XMLHTTP query, using MSXML 6.0 (not the .net natve XML).

Each node in the list looks like the attached code.

Is it possible to use xpath to return the data for each node?

For example, how do I get the friendly version ID?

I tried:
MSXML2.IXMLDOMNodeList versionNode = objNode.selectNodes("a:propstat/a:prop/e:FriendlyVersionID");

But this returns:
Reference to undeclared namespace prefix: 'a'.

How can I get around this in MSXML?  I can post more code if required.


<a:response xmlns:a="DAV:">
  <a:href>http://NHC-SP1/MedicalRecords/Documents/Medical%20Records/Patient%20Records/2001/Person%20-%20Care%20Plan%20-%20Standard%20-%2022-07-2008.doc</a:href> 
  <a:propstat>
    <a:status>HTTP/1.1 200 OK</a:status> 
    <a:prop>
      <a:href>http://NHC-SP1/MedicalRecords/Documents/Medical%20Records/Patient%20Records/2001/Person%20-%20Care%20Plan%20-%20Standard%20-%2022-07-2008.doc</a:href> 
      <a:displayname>Person - Care Plan - Standard - 22-07-2008.doc</a:displayname> 
      <a:isfolder xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" b:dt="boolean">0</a:isfolder> 
      <e:FriendlyVersionID xmlns:e="urn:schemas-microsoft-com:publishing:">1.0</e:FriendlyVersionID> 
     </a:prop>
    </a:propstat>
  </a:response>

Open in new window

0
Comment
Question by:colly92002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 1000 total points
ID: 26295125
this code uses a function called LookupNode.
1st param is nodes list
2nd param is the lookup node
3rd param is whether to use namespace prefix for lookup.

in the example i used namespace, but u could remove the "e:" and use it like this:
 string result = LookupNode(msxml.childNodes, "FriendlyVersionID", false);
static void Main(string[] args)
        {
            DOMDocument60 msxml =
                  new DOMDocument60();
            msxml.async = false;
            msxml.resolveExternals = false;

            // load XML
            msxml.load(@"C:\temp\1.xml");
            if (msxml.parseError.errorCode != 0)
            {
                // some kind of parsing error found..
                throw new Exception("parsing error:  " +
                         msxml.parseError.reason);
            }

            string result = LookupNode(msxml.childNodes, "e:FriendlyVersionID", true);
            if (result != null)
            {
                Console.WriteLine(result);
            }
        }

        private static string LookupNode(IXMLDOMNodeList nodes, string lookup, bool ns)
        {
            foreach (IXMLDOMNode node in nodes)
            {
                if (ns ? lookup == node.nodeName : lookup == node.baseName) return node.text;

                if (node.childNodes != null && node.childNodes.length > 0)
                {
                    string result = LookupNode(node.childNodes, lookup, ns);
                    if (result != null) return result;
                }
            }

            return null;
        }

Open in new window

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1000 total points
ID: 26302513
You can also do this with proper namespace resolution:
        static void Main(string[] args)
        {
            DOMDocument60 msxml = new DOMDocument60();
            msxml.async = false;
            msxml.resolveExternals = false;

            msxml.setProperty("SelectionNamespaces", "xmlns:x='urn:schemas-microsoft-com:publishing:'");

            msxml.load(@"C:\test.xml");

            IXMLDOMNode node = msxml.selectSingleNode("//x:FriendlyVersionID");

            if (node != null)
            {
                Console.WriteLine(node.text);
            }

            Console.ReadLine();
        }

Open in new window

0
 
LVL 15

Author Closing Comment

by:colly92002
ID: 31676157
Thanks, this has resolved my issue.  I can see a use for both solutions, so points split.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question