XML Parsing and Checking the syntax

Hi,

The following code will help you in parsing your XML file and also check whether it is in the correct format or not.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;

namespace XMLP
{
///
/// Summary description for Form1.
///

public class XMLP : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;

string FileName = string.Empty;
string NewFileName = string.Empty;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public XMLP()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(16, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 23);
this.button1.TabIndex = 0;
this.button1.Text = "&Open XML File";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Enabled = false;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(16, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Parse File";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// XMLP
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(154, 107);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "XMLP";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "XMLP";
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new XMLP());
}

private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog ope = new OpenFileDialog();
ope.Filter = "XML Files(*.xml)|*.xml|All Files(*.*)|*.*";
if (ope.ShowDialog() == DialogResult.OK )
{
FileName = ope.FileName;
button2.Enabled = true;
}
else
{
button2.Enabled = false;
}
}

private void button2_Click(object sender, System.EventArgs e)
{
if( FileName == string.Empty ) return;
this.Cursor = Cursors.WaitCursor;
string nsURI=string.Empty;
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(@FileName);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
nsmgr.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
nsmgr.AddNamespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
nsmgr.AddNamespace("z", "#RowsetSchema");

ArrayList ColumnList = new ArrayList();

XmlNodeList nodes = xmldoc.SelectNodes("//s:Schema/s:AttributeType", nsmgr);
foreach (XmlNode node in nodes)
{
ColumnList.Add(node.Attributes["name"].Value);
}


XmlNodeList ndlist = xmldoc.SelectNodes("//rs:data/z:row", nsmgr);
if (ndlist == null || ndlist.Count.Equals(0))
{
ndlist = xmldoc.SelectNodes("//rs:data/row", nsmgr);
}

foreach (XmlNode node in ndlist)
{
XmlNamedNodeMap mp = node.Attributes;
XmlElement nd = xmldoc.CreateElement(node.Name);

for (int i = 0; i < ColumnList.Count; i++)
{
try
{
if (ColumnList[i].ToString() == mp.Item(i).Name.ToString())
{
nd.SetAttribute(mp.Item(i).Name.ToString(), mp.Item(i).Value.ToString() );
}
else
{
string str = ColumnList[i].ToString();
nd.SetAttribute(ColumnList[i].ToString(), mp.GetNamedItem(str).Value.ToString());
}
}
catch (Exception ex)
{
nd.SetAttribute(ColumnList[i].ToString(), String.Empty);
}
}
XmlNode parent = node.ParentNode;
parent.RemoveChild(node);
parent.AppendChild(nd);
}
this.Cursor = Cursors.Default;

// Remove the Schema Details
//XmlNodeList parList = xmldoc.SelectNodes("//s:Schema", nsmgr);
//foreach(XmlNode pnode in parList)
//{
// pnode.RemoveAll();
//}

XmlNode y= xmldoc.GetElementsByTagName("s:Schema")[0];
xmldoc.DocumentElement.RemoveChild(y);

// Remove the Contect, Ttitle ect.. From the Header
xmldoc.DocumentElement.RemoveAllAttributes();

//Writing to file
SaveFileDialog sav = new SaveFileDialog();
sav.Filter = "XML Files(*.xml)|*.xml";
if( sav.ShowDialog() == DialogResult.OK )
{
NewFileName = sav.FileName ;
xmldoc.Save(NewFileName);
MessageBox.Show("File Realigned Successfully ! ");
}
}
catch(Exception ex)
{
MessageBox.Show("Unable to Parse the XML File !");
}
}
}
}


Attachments

  • XML Parsing and Checking the syntax (28089-291210-XMLP.cs.txt)
  • Comments

    Author: rpkumar08 May 2010 Member Level: Bronze   Points : 1

    thanx its really helpful
    Programmer
    Richonet Technologies Pvt. Ltd
    http://www.richonet.com



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: