I’m building a Windows 8 app in C# to show RSS feeds into the App http://blogs.msdn.com/b/jasonz/rss.aspx . This works fine when I’m reading the RSS feed and convert all content to text and bind all the feed items to the XAML properties. The next step was to convert the (x)HTML to XAML so the layout and markup would be translated and shown in a RichTextBlock.
To accomplish that I’m using this code ( https://github.com/MacawNL/WinRT-RichTextBlock.Html2Xaml ) to bind and convert html to xaml to a RichTextBlock. Which seems to work fine when I’m using a string var with some sample HTML in it. var content = “test and test number two”; Also the test and example strings work fine.
But as soon as I try to put my RSS feed content (with (x)HTML) into the WinRt-RichTechtBlock.Html2Xaml I get an exception error on this line: xhtmlDoc.LoadXml(xhtml); in RichTextBlockProperties.cs .
This is the Exception Error
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code Additional information: Value does not fall within the expected range.
I’ve been trying to find a way to parse / load my downloaded content from the RSS feed into the xhtmlDoc.LoadXml function but with no success.
How can i get WinRT-RichTextBlock.Html2Xaml to understand the content from the RSS feed to it can convert the HTML to the correct XAML tag?
Update
The xHTML could indeed be invalid (as you can see when you look at the RSSfeed) it's just some parts of HTML not the whole document). But I want to ignore those errors, and handle the correct ones it can find.
The error was because of invalid HTML. As soon as I stripped the invalid parts (e.g. target=_blank vs target="_blank" ) the exception error disappeared.
Related
I have a C# ashx handler that will do some processes and then generate an XML output which should be parsed and sent to the server.
The problem is when I try to set the context Response to txt/xml it will generate the following error:
This page contains the following errors:
error on line 1 at column 226: attributes construct error
Below is a rendering of the page up to the first error
It may look that the problem is with generated XML which may be not formatted well, but I've checked that by getting the output as a string then I used a validator and it was fine.
Any suggestions please to figure it out?
XML Output:
<Response>
<GetDigits action='http://domain/Handler.ashx?ivrlevel=9&language=arb&isOperation=false&dayFrom=3&dayTo=3&hourFrom=12&hourTo=2&minFrom=30&minTo=20&enteredMobileNumber=66355356'>
<Play>http://domain/AllClips/enterednumberis_arb.mp3</Play>
<Play>http://domain/AllClips/6.mp3</Play>
<Play>http://domain/AllClips/6.mp3</Play>
<Play>http://domain/AllClips/3.mp3</Play>
<Play>http://domain/AllClips/5.mp3</Play>
<Play>http://domain/AllClips/5.mp3</Play>
<Play>http://domain/AllClips/3.mp3</Play>
<Play>http://domain/AllClips/5.mp3</Play>
<Play>http://domain/AllClips/6.mp3</Play>
<Play>http://domain/AllClips/numberconfirmation_arb.mp3</Play>
</GetDigits>
I am getting the following warning message, What is the best solution to remove these warning messages?
This XML file does not appear to have any style information associated with it. The document tree is shown below.
I am returning data in JSON format
string JSONresult = JsonConvert.SerializeObject(dtbrands);
return JSONresult;
It's just a feature of your browser. When you request an XML file direct, the XML can specify an XSLT (the "style") which the browser can use to render html from the xml. If there is no link to an XSLT then then browser has no option but to show you the raw XML which is probably what you're seeing.
I am working on a project in visual studio that imports a CSV, and exports an XML file. I'd like to be able be able to get the code to work as XML and HTML, and view it in a browser. I am getting this error when I load the XML file into a browser:
Firefox
XML Parsing Error: not well-formed
Location: file:///C:/Users/fenwky/XmlDoc.xml
Line Number 2, Column 6:<?xsl:stylesheet <abc:stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">?>
Chrome
This page contains the following errors: error on line 2 at column 16: colon are forbidden from PI names 'xsl:transform'
This is what my c# code looks like in visual studio 2013:
// Create a procesing instruction.
XmlProcessingInstruction newPI;
// Stylesheet
String PItext = "<abc:stylesheet xmlns:abc=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">";
newPI = doc.CreateProcessingInstruction("abc:stylesheet", PItext);
doc.InsertAfter(newPI, doc.FirstChild);
// Save document
doc.Save(xmlfilename);
If you are trying to insert an processing instruction into the XML, the data parameter of the CreateProcessingInstruction method does not need to contain the name of the processing instruction in this case. In other words you just need to do this...
var PItext = "xmlns:abc=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"";
var newPI = doc.CreateProcessingInstruction("abc:stylesheet", PItext);
doc.InsertAfter(newPI, doc.FirstChild);
However, I am wondering why you are trying to add this particular processing instruction to an XML document. Perhaps you mean to link an XML document to a separate XSLT document, so it will be transformed if read by a browser?
If so, you probably need to be doing this...
var piText = "type=\"text/xsl\" href=\"style1.xsl\"";
var newPI = doc.CreateProcessingInstruction("xml-stylesheet", piText);
doc.InsertAfter(newPI, doc.FirstChild);
This will write the following processing instruction to the XML, which can then be read by the browser:
<?xml-stylesheet type="text/xsl" href="style1.xsl"?>
I use WebBrowser to display generated XML. My XML string loaded into browser by call to NavigateToString:
var text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ Environment.NewLine
+ "<whatever/>";
Browser.NavigateToString(text);
After browser loads string content I'm trying to search for any displayed text using standard Ctrl+F search dialog - but it always shows warning "No matches found".
If I save the XML string to a file and use Browser.Navigate(filename) it works.
Any ideas?
When you navigate to a file, the WebBrowser control performs MIME-type sniffing (often using the file extension as a hint). Then it creates an Active Document object of the corresponding type. Most often it's an instance of MSHTML Document, but can also be an XML, PDF or Word document, all of which support Active Document interfaces.
Now, when you navigate to a string with NavigateToString, the WebBrowser doesn't make any attempts to recognize the document type, and simply creates and instance of MSHTML Document (rather than XML Document), then tries to parse the content as HTML and fails.
I don't think you can get the desired behavior using NavigateToString, and I believe the same applies to NavigateToStream. To illustrate what's going on, take your XML content and save it as filename.html, filename.txt and filename.xml. Try opening each file with IE.
On a side note, when you navigate to a URL, the server actually has an option to suggest the MIME type, using HTTP headers. The browser may or may not tolerate such suggestion (it will still perform some validation checks).
The bottom line: you will not be able to render XML with NavigateToString or NavigateToStream. You're going to have to convert it to HTML first (e.g., with an XSLT transform).
I just had the same problem.
There is even the possibility to open the xml file directly using the overload:
webbrowser.Navigate(string filepathToXML)
Going this way, the builtin search panel works like a charm.
I am working on csv downloader project ,i need to download the CSV files generated on the webpage . and using html agility , i found the exact link that contain the link for csv file
Download file in csv format
now i want , without any activity from my side , the application must detect this link in the web page ( i could do it by Htmlagility ) and should download the file once the web page fully navigated in Web browser in my app. I tried some example in one of the SO click here post but getting
Error :Object reference not set to an instance of an object.
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links) // this ex is given another SO post
{
if (link.InnerText.Equals("My Assigned"))
link.InvokeMember("Click");
}
Can any body suggest how to do it ??
Solved :
I changed to HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A"); to HtmlElementCollection links = webBrowser1.Document.Links and used
if (link.InnerText.Contains("My Assigned"))
{
link.InvokeMember("Click");
}
. any one who better solution?
InnerText might be null so build in a safeguard, to check for null:
if ((link.InnerText != null) && (link.InnerText.Equals("My Assigned")) )
link.InvokeMember("Click");
Actually, I would get rid of HTMLAgility pack (its pretty bad) and just go/loop through it yourself. Also, don't use innerText, because based on your examples, there doesn't seem to be an innertext in at least one of the links. Use the .href attribute and check for the .csv extension.
link.href.EndsWith(".csv")
And if there are more than one .cvs on each page, look for some url string or innertext property to refine it.
Also, the reason why your .GetElementsByTagName("A") was not working was because TagName refers to the name attribute of any particular TAG. So, you were saying, Get all TAG's with the TagType name="A"... does that make sense? I think there is a .GetElementsByTag[Type] or something like that which you can use to base it on the tag type and not the name attribute of a TAG.
Also, how are you downloading the .csv file? Is a "download dialog" box coming up or are you just showing people in the webbrowser control? (curious how you've handled that part).