Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

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

0
colly92002
Asked:
colly92002
2 Solutions
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now