Skip to main content

Related Articles

How To Validate XML Document With ASP.NET

In my previous article, we discussed on how to generate XML document or XML stream from your dataset. There I mentioned that one of the major advantage of converting a dataset into XML is we can easily validate our XML data against the XML schema. The XML schema is used to define the format, data types and key constraints that you want to validate. The XML document or XML stream can be validate against the predefined XML schema and exactly you can get the validation error and on which tag that error is occurred.

First you need to have a XML document to validate. There are several ways to convert your data into XML documents. In previous article I showed you one such method. In this article I will show you how to do a validation with XML schema for existing XML document.



Step 1:

Define you XML schema that you need to validate your data with it. In Microsoft Visual Studio 2005 it very easy to define your XML schema since they have provided a graphical tool. First add XML schema file to your project solution. Please note that both XML Schema and Dataset file extensions ".xsd" since they have used common technologies. But here you should only add XML schema file not Dataset file.


Fig 1: Add XML Schema File to Solution
Using XML Schema Toolbox you can define element, attribute, attributeGroup, complexType, simpleType, group, facet, key and relationship in you schema.
Fig 2: XML Schema Toolbox
Fig 3: Sample XML Schema
Once you draw your XML schema here it will automatically generate XML code for the schema.
Step 2:
Next step is to load your XML file and validate it against the schema defined above. You need to load both XML document and XML schema into memory stream. Add resolver to pass security if accessing web site to receive schema. Last step is validation of XML document. You need use to following namespace in you coding.
using System.IO;
using System.Xml;
using System.Xml.Schema;
FileStream is used load XML file and define XmlDocument called "xdoc". XmlReaderSettings object is used to set XML Schema, XMLResolver and ValidationType. Load() method is used to load XML document and Validate() method is used to validate the XML document.
//load instance into memory
FileStream fs = File.Open("../../XMLFile1.xml", FileMode.Open);
XmlDocument xdoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, "../../XMLSchema1.xsd");

//add resolver to pass security if accessing web site to receive schema
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
settings.XmlResolver = resolver;
//this line is necessary for validation to work
settings.ValidationType = ValidationType.Schema;
ValidationEventHandler veh = new ValidationEventHandler(xvr_ValidationEventHandler);
settings.ValidationEventHandler += veh;
XmlReader reader = XmlReader.Create(fs, settings);
xdoc.Load(reader);
xdoc.Validate(veh);
ValidateEventHandler is used to define what action to take if the XML document is invalid. In this example I have written separate method called "xvr_ValidationEventHandler" to fire when the xml schema is not valid. Inside this method you write what actually you want to do if XML file is not valid according to the XML schema.
private void xvr_ValidationEventHandler(object sender, ValidationEventArgs args)
{
// The xml does not match the schema.
this.richTextBox1.Text = args.Exception.InnerException.ToString();
}
In this example I have written the Exception message into rich text box once the XML document is invalid.

Try and see how easy to work with XML language.

Comments

Popular posts from this blog

Angular PrimeNG checkboxes styling and events handling

  Getting Started With PrimeNG Styling in Angular App This chapter I am planning to show how you can handle events of the checkboxes. For that I will use checkbox for every card to place the order.  To use PrimeNG checkboxes you have to import checkbox module in app.module.ts file. import {CheckboxModule} from 'primeng/checkbox'; import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { LandingComponent } from './modules/landing/landing.component'; import { HomeComponent } from './modules/home/home.component'; import { CardModule } from 'primeng/card'; import {CheckboxModule} from 'primeng/checkbox'; @NgModule({ declarations: [ AppComponent, LandingComponent, HomeComponent ], imports: [ BrowserModule, AppRoutingModule, CardModu

The Power of ChatGPT and Whisper Models

A Deep Dive into Natural Language Processing Natural Language Processing (NLP) has seen a significant boost in recent years due to advancements in artificial intelligence and machine learning. Two models that have shown remarkable success in NLP are ChatGPT and Whisper. In this article, we will delve into the power of these models and their applications in the field of NLP. ChatGPT is a transformer-based language model developed by OpenAI that uses unsupervised learning to predict the next word in a sentence based on the context of previous words. ChatGPT is a generative model that is trained on large datasets of text, such as books and articles, and can be fine-tuned for specific tasks, such as question-answering or dialogue generation. ChatGPT is known for its ability to produce human-like text, making it an ideal tool for applications such as chatbots, content creation, and language translation. Whisper, on the other hand, is a paraphrasing model developed by Google that is based on

Getting Started With PrimeNG Styling in Angular App

  This chapter I am going to explain how you can do the styling to our flower store app. There are different kind of CSS frameworks you can use to style you app. Such as: 1.        Angular Material 2.        ngx-bootstrap 3.        NG bootstrap 4.        Prime NG   I am planning to use PrimeNG styling framework to style our flower store app. PrimeNG has more than 80 UI component and it is a collection of rich UI components for Angular. All widgets are open source and free to use under MIT License.   How to Install Prime NG 1.     Run   npm install primeng –save  inside your root folder.                    2.        Run npm install primeicons –save to install prime icons. 3.        Run npm install font-awesome --save to Install Font Awesome 4.        Run npm install @angular/cdk –save to install angular  component dev kit. Now we are done with installing Prime NG to out flower store app. If you check your package json you will see below list of libraries.