Display the contents of an XML file in an MVC view - c#

I have a "Terms and Conditions" block in my MVC view. I want the contents to be loaded from XML file (in HTML format).
How do we make it possible?

If your HTML is written directly in an XML file with no extraneous markup (which I am assuming is the case as you didn't state otherwise), you can use this line of code:
#MvcHtmlString.Create(XDocument.Load(#"filepath").ToString()));
Which will spit it out directly onto the page. You need to include this using to make use of XDocument:
using System.Xml.Linq;

You can use XSLT.
XSLT (Extensible Stylesheet Language Transformations) is a language
for transforming XML documents into other XML documents,1 or other
objects such as HTML for web pages, plain text or into XSL Formatting
Objects which can then be converted to PDF, PostScript and PNG.[2]
http://www.w3schools.com/xsl/
Cheers.

Related

Escape an xml string while creating xml file

I need to create a xml file which is to be converted to an excel file(.xls), and this means that the xml has a lot of meta info in it. Its easy to write all the contents into the xml file as a text file.
var sw = new FileInfo(tempReportFilePath).CreateText();
sw.WriteLine("meta info and other tags")
However, this method does not escape characters, and when the data contains '<' or '>' or '&' etc. the xml is rendered invalid and the .xls file does not open. I can easily do a replace ( '<' with '<' and so on), but for performance reasons, this method is not suitable.
The other alternative is to use xml text writer, but with a ton of meta info, it will mean writing a lot of tags in code. With sw.WriteLine('stuff'), I could simply put parts of meta info in one tag (as a string) and write them to file. Using xslt, the problem I faced was that tags required spaces. For example, for tabular data, the top row fields could have spaces.
How to go about creating a well formed xml file with a lot of meta info, and where the chareacters ('<', '>' etc) are excaped?
Uri.EscapeDataString(string stringToEscape);
XDocument tutorials.
Why not create xls in the first place, there is a nice library to do so :
http://npoi.codeplex.com/
I used the WriteRaw method for writing the meta info tags. For the other data, which was required to be escaped, I used WriteString method.

How do I use Linq-to-XML on the contents of a string (not document)?

Hi and thanks for looking!
Background
I am working on a developer tool for our dev team that parses content from MS Word into a Windows form with text boxes. We do some processing on the text, then submit the form to a database.
Some of the textboxes in the form contain Word XML which we need to clean up and convert to our own XML to later use with XSLT.
When the form populates, I would like to take the Word XML and use Linq to search for certain tags (example: <w:t>SOME TEXT</w:t>) and convert it to our own XML (<Text>SOME TEXT</Text>) before it gets to the textbox.
Question
How do I use Linq-to-Xml on the contents retrieved from a string in the pre-processing stage? I know how to instantiate an XDocument, but this is just a string so I am stumped. Probably missing something simple.
Thanks!
You can use the XDocument.Parse Method to create an XDocument from a string.

XDocument saves & instead of just &

I am using XDocument to switch a value in an xml document.
In the new value I need to use the character '&' (ampersand)
but after XDocument.save() the xml has & instead!
I tried using encoding and stuff… nothing worked
XDocument is doing exactly what it's supposed to do.
& is invalid XML. (it's an unfinished character/entity reference)
& means "Start of an entity" in XML so if you want to include an & as data you must express it as an entity — & (or use it in a CDATA block).
What you describe is normal behaviour and the XML would break otherwise.
There are two options. Either to ensure proper XML encoding/decoding of all your content in the XML document. Remember that HTML and XML encoding/decoding is slightly different.
Option two is to use base64 encoding on whatever content in the xml that might contain invalid elements.
Is your output file app.config supposed to be an XML file?
If it is, then the & must be escaped as &.
If it isn't, then you should be using the text output method instead of the xml output method: use <xsl:output method='text'/>.
PS: this question appears to be a duplicate of How can I add an ampersand for a value in a ASP.net/C# app config file value

xml to html to xml

I have a big xml string that needs to be displayed as a web page. I can achieve this with xslt. Now the users will make changes to certain attributes of the xml displayed on the web page.
When they are done I need to save it back in the same xml format with the modified values.
Please guide me on what would be the best method to handle this.
using asp.net + c#
I've tried something like this in the past, and resorted to using two separate XSLT sheets, one to transform to (X)HTML, and another to transform the edited one back.
Unfortunately there isn't a 'generic' way of doing it, XSLT is a one way transform; e.g. if a stylesheet disregards an element altogether, there's obviously no way of writing an inverse XSLT that will restore it.
Another possibility is to have your XML->HTML stylesheet generate id attributes on input elements in the HTML, and give the value of that attribute a value that can easily be used as a lookup in your source XML. Then you can probably just iterate through each such element in the HTML, and lookup the related element in the source and replace the value. Or the other way round, go through each element in your source, and find the value in the HTML, either works.
Take a look at this utility:
http://www.chilkatsoft.com/refdoc/csHtmlToXmlRef.html
It might be possible to use XSLT to transform it back to HTML too, but without seeing the markup it's tough to tell.

Maintaining standalone > and < symbols to load in XML when the contents also have HTML tags

We have an editor where users can have HTML formatting. We need to have those standalone < and > symbols in the XML when we load the content. I can replace < and > with their character entity references as described here but that would also include those characters in the HTML tags. Could anyone provide any direction on the same? I am using C# as the language.
It would be convenient if your editor handled escaping entity references for you. In the ASP.NET world, I use the TinyMCE editor, which handles escaping entity references nicely, and you'll find plenty of other editors that will do the same whether you are dealing with web forms or some other .NET technology.
As for HTML storage in XML, I find it easier to stuff the HTML content in a CDATA section, just to avoid the hassles of encoding/decoding content.
You need to use a CDATA area.
<XMLtag><![CDATA[<html>in here <img src="unclosed tag"> </html>]]</XMLtag>
That's < ! [ CDATA [ stuff you don't want parsed ] ]
UPDATE:
If your HTML is proper XML (all tags closed, etc) then don't worry about it, just include it in the XML doc.
If it isn't proper XML, then you can't iterator through the nodes as you go through the XML -- you will need to extract the HTML as a long text block (see above), load it into a HTML aware parser, and then iterator through that. The HTMLAgility Pack can handle iterating through impure Html.

Categories