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
Related
If I have a XSD and want to make sure it follows all the standards so that it creates a valid XML. How would that be done? The only sources I can find is back checking the XML to the XSD.
We would like to read information out of an xsd file in order to create a table with the same structure.
Is the way to do it just opening the file as an xml file, then parsing it, or is there a better way to do it?
XmlSchema.Read Method: http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschema.read.aspx
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.
I have a requirement to hand-code an text file from data residing in a SQL table. Just wondering if there are any best practices here. Should I write it as an XMLDocument first and transform using XSL or just use Streamwriter and skip transformation altogether? The generated text file will be in EDIFACT format, so layout is very specific.
The normal thing to do is just write the EDIFACT data directly.
Creating it as an XMLDocument and transforming it to EDIFACT might be useful if there's a library already available to do the transformation. I say this because there's a lot of language support for XML output.
I can't see how XSL will help you here, but I've never had to output EDIFACT data.
http://www.stylusstudio.com/edi/XML_to_EDIFACT.html
This URL has an example XSLT for translating XML to EDIFACT which might solve your problem.
I need a way to store data inside xml files and write to differant parts of the file, as well as add elements and structure to the xml document.
I need full control over the file names and xml documents, and it would be much easier if I could use some kind of SQL layer to read and write from the xml.
Just due to project constraints I am tied into using XML, but if possible would like a trust and tested open source solution for this.
Or should I be using out of box .net functionality for this?
you should be using out of box .net functionality.
the XML namespaces and Linq-to-XML will do this for you.