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)

How to Create a XML attribute through X++ code "<AxTableField xmlns="" i:type="AxTableFieldString">"

(0) ShareShare
ReportReport
Posted on by 75

Hi All,

 How to create A XML file, where in Subtags consist of Attribute as below 

<Fields>

<AxTableField xmlns="" i:type="AxTableFieldString">""

</Fields>

I have used below code and refer my output.

Code :   {

xmlElementAxTableField = nodeTableFields.appendChild(xmlDoc.createElement("AxTableField"));
xmlElementAxTableField.setAttribute2('type','i','AxTableFieldString');

}

output :

<AxTableField xmlns:d3p1="i" d3p1:type="AxTableFieldString"/>

AttributeiType.png

*This post is locked for comments

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

    For reference, I replied to the same question in the thread SetAttribute of xmlelement is not including complete string name during XML export.

    Gopal, you've attached tags for both "Microsoft Dynamics AX (current version)" (= Dynamics 365 for Finance and Operations) and AX 2012. Which one are you actually talking about?

  • GopalRC Profile Picture
    75 on at

    Hi Martin,

    Actually i'm trying to create a table through X++ code, to generate a XML file in D365 format which create a table in D365 VS IDE. As part of it i'm facing issues to create an attribute for a fields in format 

    <AxTableField i:type="AxTableFieldString" xmlns="">

    When i looked into the standard table XML it looks like below.

    AttributeiType2.png

    based on your suggestion on previous reply link (https://community.dynamics.com/ax/f/33/t/251536).  It doesn't seems to work in my case.

     I have used XMLElement.SetAttribute2() as fallows here i have pasted my logic.

    Input:
    nodeTableFields = nodeAXTable.appendChild(xmlDoc.createElement('Fields'));
    xmlElementAxTableField = nodeTableFields.appendChild(xmlDoc.createElement('AxTableField'));
    xmlElementAxTableField.setAttribute2('type','AxTableFieldString','');


    Output:
    <Fields>
    <AxTableField d3p1:type="" xmlns:d3p1="AxTableFieldString" />
    </Fields>

    Kindly suggest for the resolution :-).

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

    Having your ERP system (a web application) generating source code for Visual Studio sounds like a strange idea to me.

    Wouldn't the right approach be creating a Visual Studio add-in and using the existing API to create a table instead of generating the file all by yourself? I see that IMetaModelService (in Microsoft.Dynamics.AX.MetaData.dll) has a method called CreateTable().

  • GopalRC Profile Picture
    75 on at

    HI Martin,

    Thank you for the reply.

    As you mentioned in the previous reply to use some .dll, could you please be more specific, which .dll I need to pick.

    Please refer below screenshot, I cannot find IMetaModelService class in dll.

    CreateTable.png

    Have explored in D365 to find such .dll's which would have similar characteristics like TreeNode classes(AX 2012). I could manage to find the only accessing/reading of D365 "Application explorer ", But no luck to create objects in D365 VS IDE. As a sample test, I was trying to create a sample table.

    CreateTable.png

    Able to create sample demo “Table” in VS for D365 through X++ code by generating XML file, And, we also have checked the file for Metadata, which gets created along with "full build of application" and it is available in the right local folder

    Note: 1. Able to create a proper table structure with Method along with code inside, Fields group and index.

              2. still have the bad luck to create Fields and relations on the table because of attribute : i:type = "xyz" xmlns = "" on-field tag node.

    Please refer https://community.dynamics.com/ax/f/33/t/288007 from my post regarding creating the object in D365.

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

    It's under Service sub-namespace, if I remember it correctly.

    Here is a crude example of creating a class:

    using Microsoft.Dynamics.AX.Metadata.MetaModel;
    using Microsoft.Dynamics.Framework.Tools.AddIns.Common;
    using Microsoft.Dynamics.Framework.Tools.Extensibility;
    using Microsoft.Dynamics.Framework.Tools.MetaModel.Core;
    using Microsoft.Dynamics.Framework.Tools.ProjectSystem;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace XppDevs.AddinsDemo
    {
        /// <summary>
        /// Demonstrates how to add a new class to the the active project.
        /// </summary>
    
        // This add-in is available from Dynamics AX > Addins menu.
        [Export(typeof(IMainMenu))]
        public class AddClassMenuAddIn : MainMenuBase
        {
            private const string addinName = "AddClassDemoAXAddIn";
            // Caption is shown to users in the Addins menu.
            public override string Caption => AddinResources.AddClassAddInCaption;
            public override string Name => addinName;
    
            #region Callbacks
            /// <summary>
            /// Called when user clicks on the add-in menu
            /// </summary>
            /// <param name="e">The context of the VS tools and metadata</param>
            public override void OnClick(AddinEventArgs e)
            {
                try
                {
                    // Find project to work with
                    VSProjectNode project = LocalUtils.GetActiveProject();
                    // An alternative way is calling ProjectHelper.GetProjectDetails(), but it's not consistent
                    // with how projectService.AddElementToActiveProject() determines the active project.
                    // ProjectHelper return the startup project, which doens't have to be the active project.
    
                    if (project == null)
                    {
                        throw new InvalidOperationException("No project selected.");
                    }
                    
                    // Create a new class
                    AxClass axClass = new AxClass()
                    {
                        Name = "MyNewClass",
                        IsAbstract = true // Set a property for demonstration
                    };
    
                    // Find metamodel service needed below
                    
    
                    // Find current model
                    //ModelInfo model = metaModelService.GetModel(ProjectHelper.GetProjectDetails().Model);
                    ModelInfo model = project.GetProjectsModelInfo();
    
                    // Prepare information needed for saving
                    ModelSaveInfo saveInfo = new ModelSaveInfo
                    {
                        Id = model.Id,
                        Layer = model.Layer
                    };
    
                    // Create class in the right model
                    var metaModelProviders = ServiceLocator.GetService(typeof(IMetaModelProviders)) as IMetaModelProviders;
                    var metaModelService = metaModelProviders.CurrentMetaModelService;
                    metaModelService.CreateClass(axClass, saveInfo);
    
                    // Add the class to the active project
                    var projectService = ServiceLocator.GetService(typeof(IDynamicsProjectService)) as IDynamicsProjectService;
                    projectService.AddElementToActiveProject(axClass);
    
                }
                catch (Exception ex)
                {
                    CoreUtility.HandleExceptionWithErrorMessage(ex);
                }
            }
            #endregion
        }
    }
    

    This is from the time when AX 7 was released for the first time and I played a bit with these APIs. It's likely that others have built something more sophisticated in recent years.

  • Verified answer
    GopalRC Profile Picture
    75 on at

    I have resolved the creating I:Type Attribute by using below line of code.

    "xmlAttributeAxTableField            = xmlDoc.createAttribute3('i','type','www.w3.org/.../XMLSchema-instance&;);"

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