Windows app to get XML data from a website containing XML data - c#

I am trying to come up with a windows form application (or WPF) developed in C#.The requirement for my app is to get user search related xml node data from a website containing xml. The application would connect to a website containing xml and grab relevant xml nodes from the website. I would then display the xml node data on my windows app. What's the best way to do this, also an extension would be to grab all the xml and store in a data tier.
An sample website I will be similar to this page
http://www.amk.ca/quotations/sherlock-holmes.xml

Not entirely sure what your questions is - are you asking how to achieve this (downloading XML), or where to best put it, or what?
To grab the XML, use something like this:
using System.Net;
WebClient client = new WebClient();
string result = client.DownloadString("http://www.amk.ca/quotations/sherlock-holmes.xml");
You get back a string of XML, which you can now parse using XmlDocument or XDocument (Linq-to-XML) - are you asking how to do this??
Or if you know what sites and what format XML you're hitting ahead of time, you could also download the XML and generate a XML schema from it, and in a second step generate C# classes from the XML schema that would be suitable for deserializing the XML string into an enumeration of e.g. Quotation classes (based on the <quotation> tag in the sample XML provided).
Update: if you have a sample XML as a file, you can use the xsd.exe command line utility to generate a XML schema from the XML, and based on that XML schema, you can create a C# class to be used for deserialization. See the MSDN docs for xsd.exe for more details.
Basically, calling xsd.exe (yourfile.xml) will generate a yourfile.xsd XML schema based on your XML input file, and running xsd.exe /c (yourfile.xsd) will generate a C# class from that XML schema.
Using that, you could deserialize your XML into a C# class in one step and then "explore" the contents of the XML by just navigating around the C# class, its properties, and its lists of subelements.
That deserialization would look something like this:
XmlSerializer deserializer = new XmlSerializer(typeof(ThatDataTypeGenerated));
object result = deserializer.Deserialize(<either a file name, or a stream or something>);
This works as long as you know ahead of time what XML type you'll be getting (so that you can generate the XML schema and C# class from it, ahead of time).
Also, you can do the first step (turn XML data file into schema) inside Visual Studio, too (menu "XML" -> "Generate XML schema"), and for the second step (turning the XSD XML schema into a C# class), you could have a look at something like Xsd2Code.

Related

Paste XML as Classes, can I put in metadata

I am using Microsoft Visual Studio.
I am using Paste Special
I am using Past XML as Classes.
The XML I have is XML another program generates.
I can force meta data into the XML. (i.e. Say when a value of 123 is a string or a number)
Can I/How do I communicate this information to XSD or the IDE?

Programatically Validating Xml Input Prior To Using XmlSerializer Deserialization?

I have an Xml Schema that I created myself and generated the corresponding C# classes using the Xsd.exe tool. My application then reads in (deserializes) an Xml document into the generated type(s).
Here is the code I use to deserialize:
XmlSerializer serializer = new XmlSerializer(typeof(MyConfig));
TextReader textReader = new StreamReader(fileName);
MyConfig config = (MyConfig)serializer.Deserialize(textReader);
textReader.Close();
XmlSerializer is very forgiving when it comes to parsing Xml. The Xml can be invalid with respect to the schema but it can still be deserialized.
What I want to do is validate the Xml prior to deserializing the file. I have Google'd how to do this and it seems straight forward.
The issue/concern I am having is I want to deliver a single .EXE file but if my program does runtime Xml validation against an Xml Schema document won't I also have to include the .xsd as part of my delivery and make sure this .xsd file is always in the same (relative) directory to the executable?
Perhaps there is a way I could embed the Xml Schema document into my compiled application that way I have don't have to find/read it from disk?
Thanks!

Create a class from a XML file and use the XML content in my application (C#)?

I have a rather complex XML file which I created a class for using the xsd tool. Now I added this created class to my project and want to access the actual XML data which is in the file. What do I do next?
Thanks :)
Next learn about xml serialization/deserialization. Here is tutorial ;)

How to validate an XML document?

My C#/.NET application reads XML files that are manually edited by the users. The allowed elements and tags are described in the application's documentation. I'm using LINQ to extract data from the XML file.
Before extracting data from the XML file, I'd like to validate it to see if it has the expected structure. If not, it would be nice to have information about what is wrong so that I can give some feeback to the user.
What's the simplest way to do this in C#?
You can validate xml files against XSD.
First you have to create Xml Schema Definition file. See example
use XML Schema Definition Tool to create XSD from XMLfile
Use this code to validate input XML using corresponding XSD
Hope this will help...
EDIT
This article explains all possible ways to validate xml, using C#
How To Validate an XML Document by Using DTD, XDR, or XSD in Visual C# .NET
IMO best option is to use XSD.
Validating Input Xml Data Files

Converting log files to XML and (X)HTML, recommendedations

I've been tasked with converting some text log files from a test reporting tool that I've inherited. The tool is a compiled C# (.NET 3.5) application.
I want to parse and convert a group of logically connected log files to a single XML report file, which is not a problem. The System.Xml classes are easy enough to use.
However, I also want to create a more "readable" file to accompany each report. I've chosen HTML, and because I like standardization, I'd prefer to do it in proper XHTML.
My question is how should I go about creating the HTML files along with the XML reports? My initial thought is to build the XML file, then to use LINQ and a simple StreamWriter to build an HTML file within my C# code. I could also use XSLT as opposed to LINQ to make the C# code easier. But since I have to compile this anyway, I don't like the idea of adding more files to the installation/distribution.
Is using LINQ going to cause me any problems as opposed to XSLT? Are there any nice HTML writing libraries for .NET that conform to XHTML? Since I have everything parsed from the log files in working memory, is there an easy way to create both files at the same time easily?
I'd create an xslt transform and just run that against the XML. Linq really isn't designed to transform XML of one schema (e.g., your report) to another (e.g., xhtml). You could brute force it, but xslt is an elegant way to do it.
I would actually recommend using XSL transform. Since you already have the XML doc. If you write a good XSL transform you will get very good results.
http://www.w3schools.com/xsl/xsl_transformation.asp
small snippet:
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(HttpContext.Current.Server.MapPath(xslPath));
StringBuilder sb = new StringBuilder();
using (TextWriter tw = new StringWriter(sb))
{
// Where the magic happens
xsl.Transform(xmlDoc, null, tw);
//return of text which you could save to file...
return sb.ToString();
}
One nice thing with using the XSLT is that you can put a processing instruction at the top of the XML that tells the user's browser how to generate the report itself:
<?xml-stylesheet type="text/xsl" href="Your_XSLT.xslt"?>
That way you don't have to have a separate step to generate the report. The user just opens the XML file directly in the browser, but they see the generated report instead.

Categories