web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Answered

How to parse XML with xpath and namespace ?

(0) ShareShare
ReportReport
Posted on by

Hello !

I can't get this code to work. I want to use xpath with selectSingleNode but because the xml has name space defined (xmlns ='http://www.test/formatimport' )  the selectSingleNode returns null. If I remove the namespace from xml the code works fine.  Which is the right code?

XmlDocument xmlDocument;
    XmlNode     xmlInformationNode;
    XmlNode     xmlTransUnit;
    XmlNode     xmlTarget;
    XmlNode     xmlRoot;
    XmlNamespaceManager XmlNamespaceManager;
    str nodename;
    str xml;


    xml = "<?xml version='1.0' encoding='iso-8859-1'?>" +
    "<Product action='create' xmlns ='www.test/formatimport' >" +    
    "<TitleCode>TTTT</TitleCode>"+
    "<Date>2012-04-26</Date>"+
    "<Edition>SI</Edition>"+
    "<SectionSizes>20</SectionSizes>"+
    "<Page>"+
    "  <PageNumber>1</PageNumber>"+
    "  <Colours>c m y k</Colours>"+
    "  <Name>Etusivu</Name>"+
    "  <Type>Etusivu</Type>"+
    "</Page>" +
    "</Product>";

    xmlDocument = new xmlDocument();
    xmlDocument.loadXml(xml);
    XmlNamespaceManager = new XmlNamespaceManager(xmlDocument.nameTable());
    
    xmlRoot = xmlDocument.documentElement();

    //This do not work when I have namespace defined in xml header
    xmlTarget = xmlRoot.selectSingleNode("//TitleCode", XmlNamespaceManager);
    xmlTarget = xmlRoot.selectSingleNode("//TitleCode");

   //This works fine when the xml doesn't have xmlns defined    
   //xmlTarget = xmlRoot.selectSingleNode("//TitleCode"); 
   nodename = xmlTarget.name();


*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    You didn't add any namespace to the namespace manager and you don't identify the namespace in your query. You should do something like this:

    xmlNamespaceManager.AddNamespace('test', 'http://www.test/formatimport');
    xmlTarget = xmlRoot.selectSingleNode("//test:TitleCode", XmlNamespaceManager);
  • FlorinF Profile Picture
    on at

    Thank you.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans