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

How to share data between components in angular

  Today I am going to talk about how we can make interaction between two different components in angular. There are two main approaches that we can follow. 1. If the two components are not relative, then you can use behavioral subjects to share data and trigger the event in one component, based on the value change in another component. 2. If the component is a child component of another component, then you can share data using @Input and @output directives Today in this chapter we will discuss on the first approach. How you can use Behavior subject to share between two components.  The BehaviorSubject holds the value that needs to be shared with other components. I will explain the concept using our flower store app. First, I will add PrimeNG text filed to the app.component.html file. To add the text field you have to import it in app.module.ts and also in app.component.ts file import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from