|
-
Aug 7th, 2008, 05:20 PM
#1
Thread Starter
New Member
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!
-
Aug 7th, 2008, 05:25 PM
#2
Re: Embedding Special Character in Element Value
have you got an example of the exact value of pXElem.Value?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2008, 05:33 PM
#3
Re: Embedding Special Character in Element Value
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2008, 05:41 PM
#4
Thread Starter
New Member
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" «BusinessName» MERGEFIELD VictimMinor «VictimMinor» MERGEFIELD "VictimFirstName" «VictimFirstName» MERGEFIELD "VictimMiddleInitial" «VictimMiddleInitial» MERGEFIELD "VictimLastName" «VictimLastName» MERGEFIELD "MailingAddress1" «MailingAddress1» MERGEFIELD "MailingAddress2" «MailingAddress2» MERGEFIELD MailingCity «MailingCity», MERGEFIELD MailingState «MailingState» MERGEFIELD MailingZIP5 «MailingZIP5» SEQ CHAPTER \h \r 1Defendant: MERGEFIELD "JuvenileLastName" «JuvenileLastName», «JuvenileFirstName» «JuvenileMiddleName»Case No. MERGEFIELD "JuvenileCaseNumber" «JuvenileCaseNumber»This is to advise you that the next proceeding in your case is a/an «EventDescription» scheduled for «EventDate» at «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
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" <%= 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</w:document>
-
Aug 7th, 2008, 06:02 PM
#5
Re: Embedding Special Character in Element Value
something like this will probably do it
vb Code:
Dim testStr As String = "MERGEFIELD ""BusinessName"" «BusinessName» MERGEFIELD VictimMinor «VictimMinor» MERGEFIELD "
testStr = Regex.Replace(testStr, "«", "<%= pDataRow(" & Chr(34))
testStr = Regex.Replace(testStr, "»", Chr(34) & ") %>")
don't forget to import regex
vb Code:
Imports System.Text.RegularExpressions
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2008, 06:56 PM
#6
Thread Starter
New Member
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.
-
Aug 7th, 2008, 07:00 PM
#7
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2008, 07:14 PM
#8
Re: Embedding Special Character in Element Value
this might work
vb Code:
Dim testStr As String = "MERGEFIELD ""BusinessName"" «BusinessName» MERGEFIELD VictimMinor «VictimMinor» MERGEFIELD "
testStr = Regex.Replace(testStr, "«", "<%= pDataRow(" & Chr(34))
testStr = Regex.Replace(testStr, "»", Chr(34) & ") %>")
testStr = "<![CDATA[" & testStr & "]]>"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 8th, 2008, 12:06 PM
#9
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|