Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

How to parse XML with xpath and namespace ?

Posted on by 170

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

  • FlorinF Profile Picture
    FlorinF 170 on at
    RE: How to parse XML with xpath and namespace ?

    Thank you.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: How to parse XML with xpath and namespace ?

    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);

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans