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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to find xml node exist or not in XML file

(1) ShareShare
ReportReport
Posted on by 765

Hi,

 In AIF Outbound Service will generate customer xml file like below. If field value is empty the xml node will not generate so while am reading this xml. i want to check weather the xml node is exist or not? 

So How to find xml node is present or not in Xml File?

This is my xml file.

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="schemas.microsoft.com/.../Message">
<Header>
<MessageId>{79725196-D1C7-4B14-894B-D32E45498F71}</MessageId>
<SourceEndpoint>Test</SourceEndpoint>
<DestinationEndpoint>TEST_APICustomer</DestinationEndpoint>
<Action>tempuri.org/.../find&lt;/Action>
</Header>
<Body>
<MessageParts xmlns="schemas.microsoft.com/.../Message">
<Test_AxdCustomer xmlns="schemas.microsoft.com/.../Test_AxdCustomer">
<CustTable class="entity">
<AccountNum>100024</AccountNum>
<CellularPhone>8324898803</CellularPhone>
<CustGroup>TC</CustGroup>
<DirPartyTable class="entity">
<PartyId>0150792</PartyId>
<DirPersonPartyDetail class="entity">
<BirthDate>12/5/2015</BirthDate>
</DirPersonPartyDetail>
</DirPartyTable>
<Address_1 class="entity">
<City>Edge</City>
<State>AR</State>
<Street>678 Cap Street</Street>
<ZipCode>1724</ZipCode>
</Address_1>
</CustTable>
</Test_AxdCustomer>
</MessageParts>
</Body>
</Envelope>

i tried below code. but it doesnot work..



FileIoPermission permission;
XMLDocument doc;
XMLNode rootNode,PurchaseNodeFieldList;
XMLNodeList PurchaseList ;
WIT_Purchase wit_Purchase;
XMLParseError xmlError;
FilePath sourceFileName;
int i=0,countItemTags;
str AccountNum,Anniversary;
#define.paramsNode("/CustTable")

;
sourceFileName = @"C:\AIF Custom Integration\Outbound\Customer.xmlError";
doc = XmlDocument::newFile(sourceFileName);
permission = new FileIoPermission(sourceFileName,'rw');
permission.assert();

doc.load(sourceFileName);
if (doc)
{
xmlError = doc.parseError();

countItemTags = doc.getElementsByTagName('CustTable').length();

for( i=0 ; i < countItemTags ; i++)
{

AccountNum = doc.getElementsByTagName('AccountNum').item(i).text();

if(doc.SelectSingleNode('AnniversaryDate').text())
Anniversary = doc.getElementsByTagName('AnniversaryDate').item(i).text();
}
}

}

Any one suggest me What i missed here..

Thanks In Advance.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    239,840 Most Valuable Professional on at

    When you use a function like selectSingleNode(), check its return value before trying to use it. If it returns null and you try to call a method on the null value, you get an exception.

  • Saravanaa Profile Picture
    765 on at

    Hi Martin,

    if(doc.SelectSingleNode('AnniversaryDate').text())

    Anniversary = doc.getElementsByTagName('AnniversaryDate').item(i).text();

    if condition doesnot work if node exist in xml file next line willn't  pass values(Anniversary)

  • Martin Dráb Profile Picture
    239,840 Most Valuable Professional on at

    Please read my reply again. It explains why the call of text() or any other method will fail and how to do it right.

  • Saravanaa Profile Picture
    765 on at

    Thank u Martin,

    if(doc.getElementTagName('AnniversaryDate').count() > 0)

    Anniversary = doc.getElementsByTagName('AnniversaryDate').item(i).text();

    its working fine ...

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 10

#2
Harisgillani Profile Picture

Harisgillani 4

#2
dserp Profile Picture

dserp 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans