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 NAV (Archived)
Answered

Manipulating Instream

(0) ShareShare
ReportReport
Posted on by

I Have a XML File in which I need to load, Edit and then read with the new changes. 

My condition is I need anything above the first <hr> to be deleted.  From what I've read I can do something like above but I'm unsure how to delete these lines and save the new version. 

My Current code looks like 


// File to read from
ImportXmlFile.TEXTMODE(TRUE);
ImportXmlFile.OPEN('\\tioga-eng\Shared\NavData_Public\Testing\PCB01213BV-00009TOP.xml');
ImportXmlFile.CREATEINSTREAM(XMLSourceStream);

// File to write no
ImportXmlFile2.TEXTMODE(TRUE);
ImportXmlFile2.WRITEMODE(TRUE);
ImportxmlFile2.CREATE('\\tioga-eng\Shared\NavData_Public\Testing\PCB01213BV-00009TOP2.xml');
ImportXmlFile2.CREATEOUTSTREAM(XMLFileOutStream);

REPEAT

// Here i need to delete lines until I find the node which starts with <hr>

//How would I write this?


UNTIL (XMLSourceStream.EOS)
OR (blnHrFound = TRUE);

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Marcellus Profile Picture
    2,735 on at

    Hi Lewishhh,

    Here is what I thought:


    // File to read from ImportXmlFile.TEXTMODE(TRUE); ImportXmlFile.OPEN('\\tioga-eng\Shared\NavData_Public\Testing\PCB01213BV-00009TOP.xml'); ImportXmlFile.CREATEINSTREAM(XMLSourceStream); WHILE NOT XMLSourceStream.EOS DO BEGIN XMLSourceStream.READ(Buffer); FileContent := FileContent + Buffer; END; ImportXmlFile.CLOSE; FileLength := STRLEN(FileContent); // Find <hr> HrPosition := STRPOS(FileContent, '<hr>'); // Copy everything from <hr> FileContent := COPYSTR(FileContent, HrPosition, FileLength); // File to write no ImportXmlFile2.TEXTMODE(TRUE); ImportXmlFile2.CREATE('\\tioga-eng\Shared\NavData_Public\Testing\PCB01213BV-00009TOP2.xml'); ImportXmlFile2.CREATEOUTSTREAM(XMLFileOutStream); XMLFileOutStream.WRITE(FileContent); ImportXmlFile2.CLOSE;
  • Community Member Profile Picture
    on at

    Thank you for your reply, what data type is Buffer?

  • Community Member Profile Picture
    on at

    And File Content

  • Community Member Profile Picture
    on at

    I'm dealing with a large text document (over 1024) how would I deal with this?

  • Marcellus Profile Picture
    2,735 on at

    Hi Lewishhh,

    They are both text. 

    Vars.jpg

  • Community Member Profile Picture
    on at

    I have done this to be able to Write into the new XML document as the File is over the text Limit. However this is creating spaces between EACH WRITE and therefore causes errors when Loading. How could I tackle this issue?

    intPosition := 1;

    IF FileLength > 1024 THEN

     intLength := 1024

    ELSE

     intlength := FileLength;

    REPEAT

     // Write the first 1024, Then next..

     FileContent2 :=  COPYSTR(FileContent,intPosition,intLength);

     XMLFileOutStream.WRITE(FileContent2);

     FileContent := DELSTR(FileContent,intPosition,intLength);

     FileLength :=  STRLEN(FileContent);

     IF FileLength > 1024 THEN

       intLength := 1024

     ELSE

       intlength := FileLength;

    UNTIL FileLength = 0;

    ImportXmlFile2.CLOSE;

  • Marcellus Profile Picture
    2,735 on at

    HI Lewishhh,

    I had this idea of writing the new file as you read the original, let me know what you think.

    HrFound - Boolean
    Buffer - Text

    // Create new file
    ImportXmlFile2.TEXTMODE(TRUE);
    ImportXmlFile2.WRITEMODE(TRUE);
    ImportXmlFile2.CREATE('\\tioga-eng\Shared\NavData_Public\Testing\PCB01213BV-00009TOP2.xml');
    
    // Open and start reading original file
    ImportXmlFile.TEXTMODE(TRUE);
    ImportXmlFile.OPEN('\\tioga-eng\Shared\NavData_Public\Testing\PCB01213BV-00009TOP.xml');
    ImportXmlFile.CREATEINSTREAM(XMLSourceStream);
    WHILE NOT XMLSourceStream.EOS DO BEGIN 
      XMLSourceStream.READTEXT(Buffer);
      
      IF NOT HrFound THEN
        HrFound := STRPOS(Buffer, '<hr>') > 0;
    
      IF HrFound THEN
        ImportXmlFile2.WRITE(Buffer);
    
    END;
    ImportXmlFile.CLOSE;


  • Community Member Profile Picture
    on at

    thank you for your answers you are a great help, new issues arise however..  This document contains a lot of html links, such as

    <td class="DataTable">

               <a href="192.168.128.90/viewComponent.cgi;>PN-1007814</a>

             </td>

    From this Line I would like to remove the hyperlink but keep the

    PN-1007815 values (the anchor) so it would look like below

    <td class="DataTable">PN-1007814</td>

  • Suggested answer
    Marcellus Profile Picture
    2,735 on at

    Hi,

    You can do that with some Regex.

    Add this function:

    Variable
      DNRegex - DotNet - System.Text.RegularExpressions.Regex.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    LOCAL RemoveHTMLTags(VAR TxtLine : Text)
    Pattern := '<.*?>';
    
    DNRegex := DNRegex.Regex(Pattern);
    TxtLine := DNRegex.Replace(TxtLine, '');


    when you are reading the lines from the file you can check if the line contains a html link and call the method like this:

    WHILE NOT XMLSourceStream.EOS DO BEGIN 
      XMLSourceStream.READTEXT(Buffer);
      
      IF STRPOS(Buffer, '<a href') > 0 THEN
        RemoveHTMLTags(Buffer);

    // Then we check for HrFound as previously...
    // Write into the file 2 if Hrfound is true..
    END;
  • Community Member Profile Picture
    on at

    Your help has been great thank you, I have been tasked with changing this now from creating a new file to using In and Outstream to edit the document, This has been my current effort simplified but I'm getting an error. 'The root node is missing.'

    IncomeText = BigText

    WriteStream = Outstream

    ReadStream = InStream

    TempBlob.Init;

    TempBlob.Blob.CREATEOUTSTREAM(writeStream);

    ImportXmlFile.TEXTMODE(TRUE);

    ImportXmlFile.OPEN('\\tioga-eng\Shared\NavData_Public\Testing\PCB01213BV-00009TOP.xml');

    ImportXmlFile.CREATEINSTREAM(XMLSourceStream);

    WHILE NOT XMLSourceStream.EOS DO BEGIN

    XMLSourceStream.READTEXT(strBuffer);

     WriteStream.WRITE(strBuffer);

    TempBlob.CALCFIELDS(Blob);

    END;

    incomeText.WRITE(WriteStream);

    TempBlob.CALCFIELDS(Blob);

    TempBlob.Blob.CREATEINSTREAM(ReadStream);

    CLEAR(xmlDoc);

    xmlDoc := xmlDoc.XmlDocument();

    xmlDoc.Load(ReadStream);

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 NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans