0

I am trying to implement a solution to display XML data on a web page. I'm making use of an ASP.NET MVC Razor view. Testing the application out I used the following:

<script type="text/javascript">
    LoadXMLString('XMLHolder', '<Rfq><IsAbove30k>true</IsAbove30k><ReferenceNumber>ReferenceNumber1</ReferenceNumber><ContactPersonName>ContactPersonName1</ContactPersonName><ContactPersonTelephoneNumber>ContactPersonTe1</ContactPersonTelephoneNumber><ContactPersonCellPhone>ContactPersonCe1</ContactPersonCellPhone><BuyerName>BuyerName1</BuyerName><BuyerTelephoneNumber>BuyerTelephoneN1</BuyerTelephoneNumber><BuyerEmailAddress>BuyerEmailAddress1</BuyerEmailAddress><ProcurementItem><Title>Title1</Title><ClosingDate>1900-01-01T01:01:01+02:00</ClosingDate><Description>Description1</Description><CaptureDate>1900-01-01T01:01:01+02:00</CaptureDate></ProcurementItem><RfqGood><DeliveryTo>DeliveryTo1</DeliveryTo><DeliveryAddress>DeliveryAddress1</DeliveryAddress><DeliverySuburb>DeliverySuburb1</DeliverySuburb><DeliveryPostalCode>1</DeliveryPostalCode><SubmissionFax>SubmissionFax1</SubmissionFax><Specification>Specification1</Specification><SubmissionFax2>SubmissionFax21</SubmissionFax2><DeliveryDate>1900-01-01T01:01:01+02:00</DeliveryDate><Good>Tools &amp; Machinery</Good></RfqGood></Rfq>')

This worked fine. The XML data I'm trying to display is saved as XML in my application database. It is passed to the view in the model. Hence, I tried the following:

<script type="text/javascript">
    LoadXMLString('RfqXmlData', @Html.Raw(Model.RfqXmlData))

At this stage I received the error:

Uncaught SyntaxError: Unexpected token <

I did notice when view the source of the web page that the XML data had linebreaks between the nodes:

XmlDataLineBreaks

So, naturally I tried removing the linebreaks:

<script type="text/javascript">
    LoadXMLString('RfqXmlData', @Html.Raw(Model.RfqXmlData.Replace(System.Environment.NewLine, ""))

This succeeded in removing the linebreaks, but I still got the same error:

XmlDataNoLineBreaks

I have not idea why this is not working, as it did work when I loaded the xml string manually in the JavaScript.

Carel
  • 2,063
  • 8
  • 39
  • 65

1 Answers1

1

A string in javascript is enclosed in '' or in "", but the example you gave as neither. If you are sure that quotation marks will not be part of the output, enclose @Html.Raw(...) with quotation marks. eg. LoadXMLString('RfqXmlData', '@Html.Raw(...)');

Use the one without line breaks.

user1600124
  • 440
  • 2
  • 11
  • If the string output of @Html.Raw(...) contains single quotes, this would break tho.. you might want to replace "'" with "\'" as well in that case. – user1600124 Aug 26 '14 at 08:10
  • I actually thought of that right after I posted the question. I tried it with `@Html.Raw("'" + Model.RfqXmlData.. + "'")`. Your way is better, though. – Carel Aug 26 '14 at 08:14