Validate XML without XSD - c#

I have an XML file coming in and need that to have a few specific tags without that I cannot process that file. How can I make sure if those tags are there or not , I tried using the XSD validation but file format keeps changing and they keep sending additional tags which I do not need to process the file , but having those additional tags does not harm my process.
Is there a way to write the XSD in a way that it only looks for a few tags and ignore the others?

You can create an xsd in which you have all of the elements you require. By default an element has minOccurs=1, which would imply that it's required. Then in order to ignore all of the rest you need to add <xs:any processContents="lax" macOccurs="unbounded"/>, which basically says that the xml may contain any number of additional elements which do not need to be validated.

Consider forgoing an XSD and instead writing XPath checks against the XML to test known-invariant properties of your XSD. XSD is better for when you have a known, relatively static grammar. Ad hoc XPath assertions or Schematron would be better for XML that can't be held do a definitive grammar.

Related

What is the proper way of creating deserialization C# classes from XML data?

I'm on a project where C# deserialization classes appear to have been generated based on XML files, rather than directly from system XSD's.
Can someone please confirm: An XSD created from this single XML file could be missing metadata as some properties may be either null or missing, depending on how the XML file is generated.
From what I understand, the proper way to do this is to generate classes directly from system XSD's rather than from XML files, and identify some issues when generating from XML's?
You are right. Optional elements or fields that are missing from the sample XML file will not appear in the generated XSD file and thus not in the deserialization classes. This is also the same for a single item appearing in the XML where a list of items was expected. Even if you have a full XML file it might be missing constraints, etc. you would have in your XSD.
My suggestion is to always start from the XSD. Then create a test XML file to test against it.

user interface to get xml element's data in c#

I am trying develop a website which provide user interface to generate XML file. The user interface will ask for data required in various XML elements. The generate XML should follow DTD specifications.
So here is what I did.
I converted DTD to XSD.
I created C# class using xsd.exe tool.
Now my question is how can I generate dynamic input boxes on the webpage that will ask for required element data from the C# class I created.
I need some way to know the required and optional elements and their data type and attribute and all from the C# class I created.
i hope you get what i am asking, thanks for looking.
Keep in mind that most of the required/optional semantics from the xsd are lost in the classes generated with xsd.exe. You basically have 2 (+1 edited later ) options:
Use reflection over your generated types to render UI elements for each property. You'll have to manually manage/define databindings
Drop the xsd.exe classes and generate your UI elements by traversing the xsd itself. That way you get way more info about optional/nullable elements, cardinality etc. Construct your resulting xml by hand (use XDocument) from your UI inputs.
The hybrid approach: Reflect over generated classes for structure (easier traversal logic. no need to handle external includes etc). Go to the xsd for the additional info (You'll need to somehow figure out where in the xsd to find your needed definitions that map to the current property)
Either way you choose this will not be a trivial task and you'll need a lot of work to make it happen. And if we're going in the realm of XSD choice elements etc. you'll soon figure out that no straight forward UI can cover all the possible scenarios

Validate XML element against schema

I need to validate a small fragment of an xml file against a schema. Essentially, I'd like to ask the question "Does element X in XML document Y conform to its type as defined in schema Z?" and if not get a message describing why. This has to account for restrictions placed on those types as well (e.g. maxLength, minInclusive).
Is this possible?
I don't know about doing this from C#, but it's easily done in XQuery or XSLT 2.0. In XSLT 2.0 it's:
<xsl:copy-of select="doc('doc.xml')//selected/element" validation="strict"/>
and in XQuery it's
validate strict {doc('doc.xml')//selected/element}
All you need is a schema-aware XQuery or XSLT 2.0 processor that runs in your chosen environment.
It turns out this was much easier than I expected. The solution was to create a new schema that contains as its root the one element I want to verify. Once this schema is added to the schemaset, you can simply validate the fragment as you would any complete document.
A microsoft knowledge article that exactly describes validating Xml fragments. This could be useful.
http://support.microsoft.com/kb/318504

Adding multiple configuration files to an application

HI,
I need to add multiple configuration files in my application. What is the easiest way to read a key value from these files? Currently I am using xmldocument class and select the node using an xpath expression. Is there any other simple way to do this in C# 2.0
I had a similar need and found this to be extremely useful and simple.
http://www.codeproject.com/KB/cs/cs_ini.aspx
It is an INI file reader and writer, you just specify the header tag and the item name and it will read or write from a file. It gives you strings and you can cast them with some try blocks.
INI is really a much simpler format than XML if you have less than fifty config options and they are not nested.
A bit of a hacky solution would be to read all of the configuration files into one in memory document then use xpath to select the right nodes.
You've tagged this as c# 2.0, do you have access to LINQ to XML? That can make your queries a lot neater.
A good way to do this could be to write a class that provides you with the information you need and serialize the object trough the XmlSerializer class. To load configuration information you can deserialize the resulting Xml back to an instance trough the XmlSerializer class.

Xml Serialization and Schemas in .net (C#)

The following questions are about XML serialization/deserialization and schema validation for a .net library of types which are to be used for data exchange.
First question, if I have a custom xml namespace say "http://mydomain/mynamespace" do I have to add a
[XmlRoot(Namespace = "http://mydomain/mynamespace")]
to every class in my library. Or is there a way to define this namespace as default for the whole assembly?
Second question, is there a reason behind the always added namespaces
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
even if there is no actual reference to any of the namespaces? I just feel they add noise to the resulting xml. Is there a way to remove them an only have the custom namespace in the resulting xml?
Third question, are there tools to support the generation of schema definitions (e.g. for all public [Serializable] classes of an assembly) and the validation of xml against specific schemas available?
If there are, would you recommend XML Schema from W3C or RELAX NG?
Just to add - the "xsi" etc is there to support things like xsi:nil on values later on - a well-known pattern for nullable values. It has to write the stream "forwards only", and it doesn't know (when it writes the first bit) whether it will need nil or not, so it assumes that writing it unnecessarily once is better than having to use the full namespace potentially lots of times.
1) XmlRoot can only be set at the class/struct/interface level (or on return values). So you can't use it on the assembly level. What you're looking for is the XmlnsDefinitionAttribute, but I believe that only is used by the XamlWriter.
2) If you're worried about clutter you should avoid xml. Well formed xml is full of clutter. I believe there are ways to interract with the xml produced by the serializer, but not directly with the XmlSerializer. You have much more control over the XML produced with the XmlWriter class. Check here for how you can use the XmlWriter to handle namespaces.
3) XSD.exe can be used to generate schemas for POCOs, I believe (I've always written them by hand; I may be using this soon to write up LOTS, tho!).
Tools,
- xsd.exe, with a command line like
xsd /c /n:myNamespace.Schema.v2_0 myschema_v2_0.xsd
I put the schema in a separate project.
liqudXML which is useful if there are several schemas, or you want full support of the schema features (DateTimes with offsets, positive/Negative decimals,), and cross platform generation.

Categories

Resources