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

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

LVL 15
colly92002Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Meir RivkinFull stack Software EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carl TawnSystems and Integration DeveloperCommented:
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
colly92002Author Commented:
Thanks, this has resolved my issue.  I can see a use for both solutions, so points split.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.