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)

Select values from XML Node

(0) ShareShare
ReportReport
Posted on by 275

Hi All

I think i have tried everything. I just can't get the value out of my node. My node has the values, but i am unable to get the values.

I have tried:

XmlNode LevelOne;

Node>
<Name1>2003</Name1>
<Name2>20030630</Name2>
<Name3>20140225</Name3>
<Name4>46944000</Name4>
<Name5>94750099</Name5>

info(strfmt("%1",levelOne.text())); // GETS all the values

info(strfmt("%1",levelOne.selectSingleNode('Name1')));
//info(strfmt("%1", this.getXMLInteger("Name1", levelOne)));
//info(strfmt("%1",this.getXMLInteger("Name1", xmlNode)));
info(strfmt("%1",levelOne.text()));
//info(strfmt("%1", xmlNode.text()));

*This post is locked for comments

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

    Your XML snippet shows five different element. What's inside levelOne variable? A parent node of those five  nodes?

    And what you want to get? The content of your five nodes as a list of values, for example?

  • Ashein Profile Picture
    275 on at

    Yes that is correct. LevelOne contains my ParentNode.

    And the "Node"> is the first child.

    I would like to select fx. Name3 and get it's value. But i don't know how. I have been told to use selectSingleNode('Name1'). But it comes out blank.

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    Let me summarize it, to be sure that I understand your situation.

    You have XML code like this:

    <ParentNode>
        <Node>
            <Name1>2003</Name1>
            <Name2>20030630</Name2>
            <Name3>20140225</Name3>
            <Name4>46944000</Name4>
            <Name5>94750099</Name5>
        </Node>
    </ParentNode>

    Your levelOne variable points to ParentNode and you want to get the content of Name3. Is that correct?

  • Ashein Profile Picture
    275 on at

    Yes, that is entirely correct.

  • Suggested answer
    Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    In that case, selectSingleNode() is the natural choice, but your XPath is wrong in the given context.

    levelOne.selectSingleNode('Name1') ask for a direct descendant called Name1, but there is none. The only direct descendant of ParentNode is Node.

    Instead of using 'Name1', you should look for 'Node/Name1' or './/Node1' (this finds Node1 anywhere in the subtree).

    If you're going to work with XML files more often, make sure you learn basics of XPath. It's really handy and you'll find plenty of tutorials on internet.

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    Alternatively, you could first get a reference to Node and then to its direct descendants, but it's just more code for the same result.

  • Ashein Profile Picture
    275 on at

    I have tried info(strfmt("%1",levelOne.selectSingleNode('Node/Name1')));

    info(strfmt("%1",levelOne.selectSingleNode('//Name1')));

    I comes up wrong, like it can't find the name of the subnode

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    I tested my code based on the scenario you confirmed. Therefore either you use my code incorrectly, or your actual context is different. In either way, I can't tell you what's wrong unless you show me your code (case 1) or give me correct information about the context (case 2).

    By the way, you forgot the period in './/Node1'. Your query ('//Node1') would look for Node1 in the entire document, which doesn't have to be any problem, but might may be.

  • Ashein Profile Picture
    275 on at

    This is the code, it fails when i try to use levelOne.selectSingleNode :

    text    = rootNode.toString();

    nodeList = rootNode.childNodes();

    for (b = 0;b< nodeList.length(); b++)

       {

              levelOne = nodeList.item(b);    

        if (levelOne && levelOne.name() ==  "Node")

               {

                      info(strfmt("%1",levelOne.text())); // It prints out all the Node Childrens value

                      info(strfmt("%1",levelOne.selectSingleNode('//Node1')));

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    Let me said that levelOne variable points to ParentNode. But your code expects that levelOne.name() == "Node", which is one level below ParentNode.

    One of these things is wrong.

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