Results 1 to 9 of 9

Thread: Embedding Special Character in Element Value

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    6

    Question Embedding Special Character in Element Value

    Beth Massi has an excellent article on merging data with docx using Visual Basic - http://blogs.msdn.com/bethmassi/arch...xml-in-vb.aspx

    In the article, she cuts and pastes the document.xml file to create an XDocument, then manually replaces the Word field delimiters (« ») with ASP field delimiters (<%= %>).

    I have developed a method that takes the path/file name of the docx merge file and programmatically extracts the document.xml, then recursively loops through the elements and does a replace on the delimiters for the element values.

    My problem is that the < and > characters are being escaped. Beth's example works like a charm with the manual replacement, but the Replace method in VB is not providing the same result. Here is the function I wrote:

    Code:
    Private Function FixDelimeter(ByRef xElems As IEnumerable(Of XElement)) As Boolean
    	Dim pXElem As XElement
    
    	Try
    
    		For Each pXElem In xElems
    		pXElem.Value = pXElem.Value.Replace("«", "<%= row(""").Replace("»", """) %>")
    		If pXElem.Elements.Count > 0 Then
    			FixDelimeter(pXElem.Elements)
    		End If
    		Next
    
    	Catch ex As Exception
    
    		Throw
    
    	Finally
    
    		pXElem = Nothing
    
    	End Try
    
    End Function
    Any suggestions for inserting the characters unescaped would be welcome!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Embedding Special Character in Element Value

    have you got an example of the exact value of pXElem.Value?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Embedding Special Character in Element Value

    i'd use regex replace

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    6

    Re: Embedding Special Character in Element Value

    .paul.

    I'm rather rusty with reg-ex. How would I do that? Thanks for your quick respose!

    before the replace:

    Code:
    220345-1140460Pima County Attorney’s OfficeJUVENILE SECTION2335 E. AJO WAYTucson, Arizona  85713-6215Phone  (520) 740-2991Fax  (520) 770-9212www.pcao.pima.govBarbara LaWallPima County AttorneyAugust 7, 2008 MERGEFIELD "BusinessName" &#171;BusinessName&#187; MERGEFIELD  VictimMinor &#171;VictimMinor&#187; MERGEFIELD "VictimFirstName" &#171;VictimFirstName&#187;  MERGEFIELD "VictimMiddleInitial" &#171;VictimMiddleInitial&#187;  MERGEFIELD "VictimLastName" &#171;VictimLastName&#187; MERGEFIELD "MailingAddress1" &#171;MailingAddress1&#187; MERGEFIELD "MailingAddress2" &#171;MailingAddress2&#187; MERGEFIELD  MailingCity &#171;MailingCity&#187;,  MERGEFIELD  MailingState &#171;MailingState&#187;   MERGEFIELD  MailingZIP5 &#171;MailingZIP5&#187; SEQ CHAPTER \h \r 1Defendant:   MERGEFIELD "JuvenileLastName" &#171;JuvenileLastName&#187;, &#171;JuvenileFirstName&#187; &#171;JuvenileMiddleName&#187;Case No.  MERGEFIELD "JuvenileCaseNumber" &#171;JuvenileCaseNumber&#187;This is to advise you that the next proceeding in your case is a/an &#171;EventDescription&#187; scheduled for &#171;EventDate&#187; at &#171;EventTime&#187; at the Pima County Juvenile Court located at 2225 East Ajo Way, Tucson, Arizona.In order to verify that this hearing will proceed as scheduled, you should call the victim notification office, the prosecutor, or his/her legal assistant at 740-2991 or 740-4500 the day before the event.  Sometimes hearings get changed.Sincerely,Victim NotificationPima County Attorney's Office

    after the replace:
    Code:
    220345-1140460Pima County Attorney’s OfficeJUVENILE SECTION2335 E. AJO WAYTucson, Arizona  85713-6215Phone  (520) 740-2991Fax  (520) 770-9212www.pcao.pima.govBarbara LaWallPima County AttorneyAugust 7, 2008 MERGEFIELD "BusinessName" <%= pDataRow("BusinessName") %> MERGEFIELD  VictimMinor <%= pDataRow("VictimMinor") %> MERGEFIELD "VictimFirstName" <%= pDataRow("VictimFirstName") %>  MERGEFIELD "VictimMiddleInitial" <%= pDataRow("VictimMiddleInitial") %>  MERGEFIELD "VictimLastName" <%= pDataRow("VictimLastName") %> MERGEFIELD "MailingAddress1" <%= pDataRow("MailingAddress1") %> MERGEFIELD "MailingAddress2" <%= pDataRow("MailingAddress2") %> MERGEFIELD  MailingCity <%= pDataRow("MailingCity") %>,  MERGEFIELD  MailingState <%= pDataRow("MailingState") %>   MERGEFIELD  MailingZIP5 <%= pDataRow("MailingZIP5") %> SEQ CHAPTER \h \r 1Defendant:   MERGEFIELD "JuvenileLastName" <%= pDataRow("JuvenileLastName") %>, <%= pDataRow("JuvenileFirstName") %> <%= pDataRow("JuvenileMiddleName") %>Case No.  MERGEFIELD "JuvenileCaseNumber" <%= pDataRow("JuvenileCaseNumber") %>This is to advise you that the next proceeding in your case is a/an <%= pDataRow("EventDescription") %> scheduled for <%= pDataRow("EventDate") %> at <%= pDataRow("EventTime") %> at the Pima County Juvenile Court located at 2225 East Ajo Way, Tucson, Arizona.In order to verify that this hearing will proceed as scheduled, you should call the victim notification office, the prosecutor, or his/her legal assistant at 740-2991 or 740-4500 the day before the event.  Sometimes hearings get changed.Sincerely,Victim NotificationPima County Attorney's Office
    the xDocument after returning from the function:

    Code:
    <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">220345-1140460Pima County Attorney’s OfficeJUVENILE SECTION2335 E. AJO WAYTucson, Arizona  85713-6215Phone  (520) 740-2991Fax  (520) 770-9212www.pcao.pima.govBarbara LaWallPima County AttorneyAugust 7, 2008 MERGEFIELD "BusinessName" &lt;%= pDataRow("BusinessName") %&gt; MERGEFIELD  VictimMinor &lt;%= pDataRow("VictimMinor") %&gt; MERGEFIELD "VictimFirstName" &lt;%= pDataRow("VictimFirstName") %&gt;  MERGEFIELD "VictimMiddleInitial" &lt;%= pDataRow("VictimMiddleInitial") %&gt;  MERGEFIELD "VictimLastName" &lt;%= pDataRow("VictimLastName") %&gt; MERGEFIELD "MailingAddress1" &lt;%= pDataRow("MailingAddress1") %&gt; MERGEFIELD "MailingAddress2" &lt;%= pDataRow("MailingAddress2") %&gt; MERGEFIELD  MailingCity &lt;%= pDataRow("MailingCity") %&gt;,  MERGEFIELD  MailingState &lt;%= pDataRow("MailingState") %&gt;   MERGEFIELD  MailingZIP5 &lt;%= pDataRow("MailingZIP5") %&gt; SEQ CHAPTER \h \r 1Defendant:   MERGEFIELD "JuvenileLastName" &lt;%= pDataRow("JuvenileLastName") %&gt;, &lt;%= pDataRow("JuvenileFirstName") %&gt; &lt;%= pDataRow("JuvenileMiddleName") %&gt;Case No.  MERGEFIELD "JuvenileCaseNumber" &lt;%= pDataRow("JuvenileCaseNumber") %&gt;This is to advise you that the next proceeding in your case is a/an &lt;%= pDataRow("EventDescription") %&gt; scheduled for &lt;%= pDataRow("EventDate") %&gt; at &lt;%= pDataRow("EventTime") %&gt; at the Pima County Juvenile Court located at 2225 East Ajo Way, Tucson, Arizona.In order to verify that this hearing will proceed as scheduled, you should call the victim notification office, the prosecutor, or his/her legal assistant at 740-2991 or 740-4500 the day before the event.  Sometimes hearings get changed.Sincerely,Victim NotificationPima County Attorney's Office</w:document>

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Embedding Special Character in Element Value

    something like this will probably do it

    vb Code:
    1. Dim testStr As String = "MERGEFIELD ""BusinessName"" «BusinessName» MERGEFIELD  VictimMinor «VictimMinor» MERGEFIELD "
    2. testStr = Regex.Replace(testStr, "«", "<%= pDataRow(" & Chr(34))
    3. testStr = Regex.Replace(testStr, "»", Chr(34) & ") %>")

    don't forget to import regex
    vb Code:
    1. Imports System.Text.RegularExpressions

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    6

    Re: Embedding Special Character in Element Value

    I just tried your suggestion, and there's no difference. It seems that the FixDelimeter function works correctly, but after it returns the xDocument itself is escaping the characters.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Embedding Special Character in Element Value

    i thought that might be the problem. can't you rewrite the file another way, using the new values?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Embedding Special Character in Element Value

    this might work

    vb Code:
    1. Dim testStr As String = "MERGEFIELD ""BusinessName"" «BusinessName» MERGEFIELD  VictimMinor «VictimMinor» MERGEFIELD "
    2. testStr = Regex.Replace(testStr, "«", "<%= pDataRow(" & Chr(34))
    3. testStr = Regex.Replace(testStr, "»", Chr(34) & ") %>")
    4. testStr =  "<![CDATA[" & testStr & "]]>"

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    6

    Re: Embedding Special Character in Element Value

    I could if I avoided the XDocuments. Thanks for all your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width