Show XML content in div innertext - c#

I have my XML as a string and need to display it in a div
I have searched all the ways to format an XML, but it doesn't work as I am finally binding my contents to a div.
Following is what I am using in my asp.net applicaion c# code.
request = //my xml string
XDocument doc = XDocument.Parse(request);
divLogResults.InnerText = doc.ToString()
I have tried all the ways listed here as well
What is the simplest way to get indented XML with line breaks from XmlDocument?
So this isn't a duplicate. How can I do this in a simple way ?

Bind your XML content in a text area. I think this should work.
Web form:
<textarea rows="20" cols="40" style="border:none;" runat="server" id="txtXML" readonly="true">
</textarea>
Code behind:
request = // xml string
XDocument doc = XDocument.Parse(request);
txtXML.Text = doc.ToString()
Please make sure your xml content has the header appended
e.g.,
<?xml version="1.0" encoding="ISO-8859-1"?>

Related

Paste HTML code from XML to page like HTML and like String

I have problem with parsing my HTML code from XML
Here some code for introducing
<item>
<title>
My HTML code
</title>
<description>
<![CDATA[Here some example
<ul style="list-style: disc;" type="disc">
<li>Text1</li>
<li>Text2</li>
</ul>]]>
</description>
</item>
I'd like show this code in 2 ways, first like html list and second like string(make visible all tags e.g...)
I tried to grab this code in 2 ways and in booth result are same
string DescriptionCurent = item.Element("description").Value.ToString();
HtmlString html2 = new HtmlString(item.Element("description").Value.ToString());
And on the end I show this on page
<p><%= DescriptionCurent %></p>
<p><%: html2 %></p>
On the end we have text before in p tag, and list outside P tag showing like regular list.
I use asp.net 4.5 web forms
If you can't understand something pls ask and I will try to explain better
UPDATE:
I cant add images because i need 10 reputation so i put it on some free host
Here is image ho that look
http://tinypic.com/view.php?pic=2eam7bb&s=8#.VQLYLY7F91A
And here it's in inspect element how look
http://tinypic.com/view.php?pic=zv205h&s=8
so I just need to add everything in tag no outside
I assume that item is an XElement from LINQ-to-XML. So, to get XML markup from an XElement object you simply call ToString() on the object (not on the object's Value property) :
HtmlString html2 = new HtmlString(item.Element("description").ToString());
Or a different way if you meant to get, specifically, the content of the CDATA element (excluding the <CDATA> tag itself) :
XCData cdata = (XCData)item.Element("description").FirstNode;
HtmlString html2 = new HtmlString(cdata.Value);

Render XML document with XSL to a HTML page

I have a xml document like below and I need to render it into HTML page. When I browse the XML from IE the HTML is rendered as expected with styling. If I load the xml document from c# code and pass to HTML page it just renders as plain text. What am I missing here?
XML
<?xml-stylesheet type='text/xsl' href='xslsheet.xsl'?>
<Document xmlns="org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
....
</Document>
C#
XDocument doc = XDocument.Load(#"C\SampleDocument.xml");
var result = doc.ToString();
Loading an XML document does just that - it loads the data. It won't process a transform directive.
To do that you need to do an XSLT Transform. You can find the classes to do that on MSDN.

Error while using XPath to parse text from HTML

The HTML content I need to parse is the text in the marquee element as given below. I'm using C# with HTML Agility Pack to parse it, but a nullrefrence exception is thrown.
C# code is
var ht1 = ht.DocumentNode.SelectSingleNode("html/body/table/tbody/tr/td[2]/div[2]/marquee/text()").InnerText;
Part of HTML:
<html>
-<body ...
-<table id=..
-<tbody>
-<tr>
+<td.........
-<td
+<div ......
-<div style="width:100%;padding:0;margin:0;border
-style:solid;border-width:0;border-color:darkred;">
<marquee width="100%" height="20" bgcolor="" style="color:
darkorchid; font-size: 14" loop="3" behavior="scroll"
scrolldelay="90 scrollamount="5" align="middle" border="0">
your scrolling text - these are some samples - think of
possibilities</marquee>
<div>
Did you look in the direct source of the html file? If you only look in the html shown in a browser like Firebug/fox, it shows additional tbody tags, that are not actually in the file.
Therefore use:
var ht1 = ht.DocumentNode.SelectSingleNode("html/body/table/tr/td[2]/div[2]/marquee/text()").InnerText;
You usually do not want to use text() because, the text content of a node is already its text. And text() returns a set of text-nodes, not the concatenated text.
Therefore use:
var ht1 = ht.DocumentNode.SelectSingleNode("html/body/table/tr/td[2]/div[2]/marquee").InnerText
That page does not seem to be well formed HTML.
This worked for me though:
ht.DocumentNode.SelectSingleNode(#"html/head/table[1]/tbody/tr/td[1]/td/div[2]/marquee").InnerText;

identify html in windows form

I have use web service in my windows application. webservice return string like:
<b>sdfsdf</b>
<img alt="*" src="df"/>
is any way in windows form that read html tag. like if <b/> then text is bold. and <img/> image should apear not text.
Easiest way would be to use a WebBrowser control, I suppose.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx
Load your XML string into an XmlDocument:
XmlDocument doc = new XmlDocument();
doc.LoadXml(webservice_output_string);
Then you can use XPath against the document:
string bold = (doc.SelectSingleNode("//b") as XmlElement).InnerText;
string src = (doc.SelectSingleNode("//img/#src") as XmlAttribute).Value;

Page.FindControl?

I am working on a page that inherits a Base Page. The aspx page includes a control that uses xslt for to transform an xml document to html markup. Within that document I am using the following:
<xsl:template match="Headline">
<h1 runat="server" id="h1" class="article-heading">
<xsl:value-of select="text()"/>
</h1>
</xsl:template>
I am trying to get the get the value of the h1 to set it to page.title, can this be done with page.findControl ?
XSLT within a browser tends to be interpreted on the client-side, not the server side. Using Page.FindControl to find the content of the H1 won't get you too far, as all that will return is the literal <xsl:value-of...> statement.
The best approach is to also open the XML document within the codebehind on the server and set the Page Title from there.
you can use javascript to find h1 on clientside then set it to the document.title
Unfortunately no, because your <h1> element never gets added to control tree on the server. Even though you have runat="server" ASP.NET doesn't parse HTML resulting from XSLT transformation.
You would have to resort to parsing your XML to get the heading. With XPath it should be easy.

Categories