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

Announcements

No record found.

News and Announcements icon
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
    239,040 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans