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)

"Exception: There are multiple root elements. Line 1, position 2." while using DataContractSerializer in consuming Query & Metadata services

(0) ShareShare
ReportReport
Posted on by

Good day guys,

i have written the code below in x++ to simply consume the Query service in addition to Metadata service inside another instance of AX.

As you know, consuming the Query service to execute static AOT query is just the easiest thing you might ever do while integrating AX, it gets a bit complected when you want to set query range (filters) and involve the metadata service though.

Anyways, i 've got everything fine and working. But with an exception that is telling "There are multiple root elements. Line 1, position 2".

here is my code part that fires the exception (specifically on the last line when calling ReadObject(memoryStream)) :

_metaDataClient = AifUtil::createServiceClient(CLRInterop::getType("NepMetaDataService.AxMetadataServiceClient"));

            metaSer = new System.Runtime.Serialization.DataContractSerializer(_queryMeta.GetType());
            querySer = new System.Runtime.Serialization.DataContractSerializer(_query.GetType());
            
            _querysMeta         = _metaDataClient.GetQueryMetadataByName(queryNames);
            _queryMeta          = _querysMeta.get_Item(0);
            
            metaSer.WriteObject(memoryStream, _queryMeta);
            memoryStream.Seek(0, System.IO.SeekOrigin::Begin);

            _query = querySer.ReadObject(memoryStream); //exception fired.


moreover, i tried to use the XmlReader and XmlWriter as shown here and got null _query object and therefore no results,

            settings.set_ConformanceLevel(System.Xml.ConformanceLevel::Fragment);
            xmlReader = System.Xml.XmlReader::Create(memoryStream, settings);
            xmlWriter = System.Xml.XmlWriter::Create(memoryStream);
            
            _querysMeta         = _metaDataClient.GetQueryMetadataByName(queryNames);
            _queryMeta          = _querysMeta.get_Item(0);
            

            metaSer.WriteObject(xmlWriter, _queryMeta);
            memoryStream.Seek(0, System.IO.SeekOrigin::Begin);
            xmlReader = System.Xml.XmlReader::Create( (memoryStream, settings);

            _query = querySer.ReadObject(xmlReader);



Although my code shown up in green gives the required result, i am tired to get rid of that exception. any help please?

*This post is locked for comments

I have the same question (0)

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