How to validate a XSD in C# - c#

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.

Related

Building not valid XML Document (Name cannot begin with the '0' character) [duplicate]

This question already has answers here:
How to parse invalid (bad / not well-formed) XML?
(4 answers)
Closed last year.
I am trying to decode a custom xml config file in C#, but I am having troubles to create this file from the string I was able to get after my decode step.
After trying to build the xml, I got this error:
System.Xml.XmlException : 'Name cannot begin with the '0' character, hexadecimal value 0x30. Line 1, position 2.'
I know my xml is not a valid xml file because of its bad formatting but I would like to know is there is a way to build it anyways.
Format of the "xml file" :
<01_config.xml>
<name dataType="String">some_name</name>
<description dataType="String">some_description</description>
</01_config.xml>
If I replace 01_config.xml by config during debug, everything will work fine since it will become a valid xml file. But it will not be the good format for my config.
I guess I can still build the file without using the C# Xml building tools, but I would like to know if it's possible to do it with it in the first place.
XML component (element and attribute) names may not begin with a number.
Strictly speaking, this is a matter of the rules for being an XML document – well-formedness, not validity.
Reasons to correct this mistake
You want your document to be XML.
You want users of your document to be able to use the XML ecosystem of editors, parsers, validators, databases, transformation/selection languages, and libraries available in many languages.
You want the interoperability benefits of using a standard.
You want to participate in a community of users – tapping into, and contribute to, a collective body of knowledge to the community's mutual benefit.
Reasons to proceed with bad "XML"
You like the aesthetics of your "XML" variant because it's a "good format for my config".
Recommendation
Fix the mistake and work with standard XML.
See also
How to have an XML tag start with a number?
How to parse invalid (bad / not well-formed) XML?
I know my xml is not a valid xml file because of its bad formatting but I would like to know is there is a way to build it anyways.
Sure, you can build files that aren't well-formed XML, but then you won't be able to read them using XML tools.

How to deserialize unknow XML in C#?

My question is : How to deserialize unknown XML to C# classes ? I know about "Paste special -> Paste XML as Classes" but i need something (code )which generate a C# classes object from XML. My program need add, remove and edit all nodes in tree XML.
You can do this by using XDocument, parsing the XML, outputting a C# CodeDom into memory, compiling it in memory. Not sure why you would want to do such a thing, but XML Parsing and CodeDom is the way to go to.

Windows app to get XML data from a website containing XML data

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.

how do i parse a dtd file

I want to parse a dtd file and use the info I get from that to create some classes. I know that I can convert it to a xsd and then parse it, but I was hoping to avoid that. Everything I find via google is to validate against a dtd. So I guess my question is: How do I parse a dtd file using c# or are there any tools or libraries out there that I can use? I should add that I'm using visual studio 2005.
You need an SGML parser: this thread should help you out: SGML parser .NET recommendations

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

Categories