Problem reading XML file

David_SingletonDavid_Singleton Member Posts: 5,479
Hi guys.

I have a problem that I can not resolve and need some help. I am trying to import an XML file with no luck. When we started the XML file looked something like this:
<?xml version="1.0" encoding="utf-8"?>
<MyHeader>
  <HeaderReport MyIDID="123456" MYOtherID="Fred_Blogs_99">
  <AddressData Street="21 Smith Street" ZipCode="12345" Town="XYZZY"> 
...

But now they are using the following format
<?xml version="1.0" encoding="utf-8"?>
<MyHeader>
  <HeaderReport>
    <MyIDID>123456</MyIDID>
    <MYOtherID>Fred_Blogs_99</MYOtherID>
  <AddressData> 
    <Street>21 Smith Street</Street>
    <ZipCode>12345</ZipCode>
    <Town>XYZZY</Town>
  </AddressData> 
...

Both are pretty standard XML formats, but when I import the second file I get an error message

The Element HeaderReport was not expected.

So it thinks that the next attribute is an element.

Shouldn't Navision just accept XML in either of these formats?

Thanks in advance, I have been banging my head trying to figure this one.

(By the way I made up the data above, so don't take it too literally, I am only concerned about the two different formats.
David Singleton

Comments

  • JedrzejTJedrzejT Member Posts: 267
    Hi,

    For me only one diffrence is that the MyIDID and MYOtherID is now elements (was attributes before)
    Should be no problem with importing after change that 2 attributes for elements.. but i suppose You do it already and still not working? You do it via XmlPort?

    I see after edit.. there is some change in structure.
    This look like wrong xml format.. HeaderReport should be ended before AddresData is started.. because it is the same indent
    <?xml version="1.0" encoding="utf-8"?>
    <MyHeader>
      <HeaderReport>
        <MyIDID>123456</MyIDID>
        <MYOtherID>Fred_Blogs_99</MYOtherID>
      <AddressData> 
        <Street>21 Smith Street</Street>
        <ZipCode>12345</ZipCode>
        <Town>XYZZY</Town>
      </AddressData> 
    ...
    



    Regards
    Jedrzej
  • David_SingletonDavid_Singleton Member Posts: 5,479
    JedrzejT wrote:
    Hi,

    For me only one diffrence is that the MyIDID and MYOtherID is now elements (was attributes before)
    Should be no problem with importing after change that 2 attributes for elements.. but i suppose You do it already and still not working? You do it via XmlPort?

    Yes its an xml port and I changed the attributes to Elements, but then got an error about incorrect indentation. I am missing something really simple here and just can't see it.

    Don't worry about the incorrect close, I just wrote that xml snipped manually into the mibuso editor. The actual file contains confidential information that I can't post.

    Thanks for your help.
    David Singleton
  • DenSterDenSter Member Posts: 8,305
    Those names are case sensitive, so make sure you get the case right. Did you check the MinOccurs and MaxOccurs properties?
  • David_SingletonDavid_Singleton Member Posts: 5,479
    DenSter wrote:
    Those names are case sensitive, so make sure you get the case right.
    That was also my first assumption, so I copy pasted the texts to make sure.
    DenSter wrote:
    Did you check the MinOccurs and MaxOccurs properties?
    In the end I went through and set them all to Zero.

    I am sure I just missed something really silly.

    Actually I when I first started, I got this error:

    Microsoft Dynamics NAV
    The XML Parser setup fails.
    Error = 80040154 Code meaning = Class not registered

    OK

    which seems to stem from installing 2009 that over writes a DLL. So I am now working on a different computer as well.

    This particular XML just hates me.
    David Singleton
  • JedrzejTJedrzejT Member Posts: 267
    I know this is silly, but if you just change TagType from attributes to element.. ,maybe good thing will be just Delete whole Xml Line and create from scratch.. there are some other properties for other TagTypes.. which is saved even if you change TagType. I will try this just to be sure..
  • SLF25SLF25 Member Posts: 37
    I had a similar problem once, the problem was that the XMLport designer wouldn't let me indent the structure properly (some strange errors). After composing the XMLport from scratch the indentation worked properly.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    JedrzejT wrote:
    I know this is silly, but if you just change TagType from attributes to element.. ,maybe good thing will be just Delete whole Xml Line and create from scratch.. there are some other properties for other TagTypes.. which is saved even if you change TagType. I will try this just to be sure..
    Good idea. I will give this a try. The issue seems to be that as an attribute for the old file, NAV (logically) gives me an "Unexpected element" error. But when I convert to Element it gives me an indentation error.
    SLF25 wrote:
    I had a similar problem once, the problem was that the XMLport designer wouldn't let me indent the structure properly (some strange errors). After composing the XMLport from scratch the indentation worked properly.
    And yes that is about where I am now, If Jedrzej's solution doesn't work then I am going to build the whole thing again.
    David Singleton
  • ara3nara3n Member Posts: 9,256
    Hello David
    Could you paste the whole xml. Preferably one with more than one record?
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • David_SingletonDavid_Singleton Member Posts: 5,479
    ara3n wrote:
    Hello David
    Could you paste the whole xml. Preferably one with more than one record?

    Unfortunately it's customer confidential information. The snippet above is just to indicate the issue between attributes and elements. I know its not really a lot to go on, I was just hoping that someone might have seen a similar issue and have a silver bullet.
    David Singleton
  • DenSterDenSter Member Posts: 8,305
    I think at this point recreating the XMLPort with the proper tagtypes would give you the best chance of success.
  • ara3nara3n Member Posts: 9,256
    I suggest to use XMLPort generator from download link.

    You need to open the xml file in visual studio. click on xml menu and select generate xsd.

    Then in XMLPortGenerator select this xsd. The tool will create the xlmport for you. Very time saving app.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • David_SingletonDavid_Singleton Member Posts: 5,479
    ara3n wrote:
    I suggest to use XMLPort generator from download link.

    You need to open the xml file in visual studio. click on xml menu and select generate xsd.

    Then in XMLPortGenerator select this xsd. The tool will create the xlmport for you. Very time saving app.

    Sorry for not getting back to you all.

    Yes Rashed this tool is brilliant. I used XSD.exe to convert xsd from the XML then created the XML. It looked identical to mine, but gave different error messages. So I think that converting from attributes to Elements (as mentioned above) there were some left over properties.

    BUT

    Still errors, now its a different Element that it does not expect, even though that element is in the XMLport.

    I think the issue has to be int he formatting of the document it self. So I have converted the current XML files to SV to import them and will start again with a clean ML file with a single record and try it from there.

    This particular xml really hates me.
    David Singleton
  • DenSterDenSter Member Posts: 8,305
    Something that I've run into is that XMLPorts are very picky about the order of the elements and attributes. The XML must have everything in the exact same order as the XMLPort, or it's going to get cranky.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    DenSter wrote:
    Something that I've run into is that XMLPorts are very picky about the order of the elements and attributes. The XML must have everything in the exact same order as the XMLPort, or it's going to get cranky.

    Yes I think that could be a problem. It looks like some parts of the file they swap the order like
    <?xml version="1.0" encoding="utf-8"?>
    <MyHeader>
      <HeaderReport>
        <MyIDID>123456</MyIDID>
        <MYOtherID>Fred_Blogs_99</MYOtherID>
      <AddressData>
        <Street>21 Smith Street</Street>
        <ZipCode>12345</ZipCode>
        <Town>XYZZY</Town>
      </AddressData> 
    ...
    MyHeader>
      <HeaderReport>
        <MYOtherID>Joe_Blogs_99</MYOtherID>
        <MyIDID>654321</MyIDID>
      <AddressData>
    


    urrrrggggggg
    David Singleton
  • DenSterDenSter Member Posts: 8,305
    that's not only 'a' problem, but that is likely 'the' problem. If your XMLPort is set up along the lines of the first file, then while importing the second file you would get the 'unexpected element' error.

    The incoming XML must always match 100% or the XMLPort will scream at you.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    DenSter wrote:
    that's not only 'a' problem, but that is likely 'the' problem. If your XMLPort is set up along the lines of the first file, then while importing the second file you would get the 'unexpected element' error.

    The incoming XML must always match 100% or the XMLPort will scream at you.

    No I mean elements in the same file. I would have assumed the XSD generator to give an error message when created the schema.

    But yes I am pretty sure that this is the root cause of all the problems. And I probably would never have found it without theXML generator.
    The problem is trying to find the incorrect sequence in the thousands of lines and then handing it back to the vendor as "Their problem". The error message just tells you there is an unexpected element but doesn't say where, not even a line number.

    I think I can solve this now though so thanks to everyone for the help.
    David Singleton
  • DenSterDenSter Member Posts: 8,305
    But yes I am pretty sure that this is the root cause of all the problems. And I probably would never have found it without theXML generator.
    Actually you would have because I pointed that out to you in my last reply. Out of order elements is the number 1 cause of the error message in your original post, but I assumed you knew that so I didn't include that in my reply to the OP. Not until you mentioned the same error with another element did it dawn on me that you didn't know about that.
Sign In or Register to comment.