The complete implementation of XML file format is divided into the following steps:
Following is a code snippet for creating an XML document:
The complete code can be downloaded from: http://keensocial.freeiz.com/blogs/xmlfileformat/xmlfileformat.zip. The project file can only be opened in Visual Studio 2010. If you have other versions of Visual Studio, you have to create a new solution and add the extracted files from the zip file to the solution.
- XML Schema (XSD) definition
- Let an extension for my file type: .xef (XML example file-type)
- Generate an XML file with data compatible to previously defined schema
- Validate my XML with the schema
- Compress huge data fragments
- Encrypt sensitive data
- Implementation in Java (Compliance with XSD 1.1)
- Create a Windows Forms application.
- Add 3 buttons
- Load
- Load and Validate
- Save
Following is a code snippet for creating an XML document:
string schemaName = "set.xsd"; XmlDocument document = new XmlDocument(); document.Load(fileName); XmlElement xmlNode = doc.CreateElement("XmlFileFormat"); XmlAttribute attr = doc.CreateAttribute("xmlns:xsi"); attr.Value = "http://www.w3.org/2001/XMLSchema-instance"; xmlNode.SetAttributeNode(attr); attr = doc.CreateAttribute("xsi:noNamespaceSchemaLocation"); attr.Value = m_schemaName; xmlNode.SetAttributeNode(attr); doc.AppendChild(xmlNode); doc.Save(fileName);And code snippet for loading this document:
string schemaName = "set.xsd"; XmlDocument document = new XmlDocument(); document.Load(fileName);The validation is done using XmlSchema class:
string schemaName = "set.xsd"; string fileName = "set.xml"; XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(string.Empty, schemaName); settings.ValidationType = ValidationType.Schema; XmlReader reader = XmlReader.Create(fileName, settings); XmlDocument document = new XmlDocument(); document.Load(reader); ValidationEventHandler eventHandler = new ValidationEventHandler(OnValidation); // the following call to Validate succeeds. document.Validate(eventHandler); reader.Close();If anything is changed in the document we can always call document.Validate() with the OnValidation event handler:
private void OnValidation(object sender, ValidationEventArgs e) { switch (e.Severity) { case XmlSeverityType.Error: Console.WriteLine(e.Message); break; case XmlSeverityType.Warning: Console.WriteLine(e.Message); break; } }
Very frequently we come to problems in XML data when we need to represent large data sets. Due to verbose tags, we need several byte even to write a single data byte. This causes an extreme overhead during serialization and data transfer over slower networks.. There were number of efforts seen in the open source and research communities to overcome this problem. In the next post, we will implement one mechanism in C# to compress large datasets.
The complete code can be downloaded from: http://keensocial.freeiz.com/blogs/xmlfileformat/xmlfileformat.zip. The project file can only be opened in Visual Studio 2010. If you have other versions of Visual Studio, you have to create a new solution and add the extracted files from the zip file to the solution.
No comments:
Post a Comment