Solved

Create Class to parse XML file

Posted on 2006-10-27
3
172 Views
Last Modified: 2010-04-16
Hi,

I have to develop a class in c# which gets xml file as input. Then it has to search for some tags. If the xml file contains <instrument> then the XML file has to move to the class instrumentconfigure().

If the xml file contains <policy> tag then it has to move to class policyconfigure() how can i do this in c#?
0
Comment
Question by:Rahamathulla_J
  • 2
3 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17819163
//Create an XmlDocument
XmlDocument doc = new XmlDocument();
//Load the xml into it from a string of xml text...
doc.LoadXml(xml);
...or from a file...
doc.Load(path_to_file);

//search the doc for any <instrument> node
XmlNode node = doc.DocumentElement.SelectSingleNode("//instrument");
/// if you find one
if (node != null){
    // do your thing
    return;
}

//search the doc for any <policy> node
node = doc.DocumentElement.SelectSingleNode("//policy");
/// if you find one
if (node != null){
    // do your thing
    return;
}
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 250 total points
ID: 17824599
using System;
using System.Collections;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace CustomXML
{
    #region Custom Xml Class
    public class CustomXML
    {
        #region Variables
        XmlWriter FWriter;
        XmlDocument FDocument;
        string FFileName;
        #endregion

        #region Constructor
        private CustomXML()
        {
            FDocument = new XmlDocument();
        }
        public CustomXML(string FileName):this()
        {
            this.FileName = FileName;
            CreateDocument();
        }
        public CustomXML(StringBuilder AXml): this()
        {
            this.FileName = null;
            LoadDocument(AXml);
        }
        #endregion

        #region Destructor
        ~CustomXML()
        {
            DisposeDocument();
        }
        #endregion

        #region Indexer
        public XmlNodeList this[string NodeName, bool All]
        {
            get
            {
                XmlNodeList FTemp;
                try
                {
                    FTemp = FDocument.GetElementsByTagName(NodeName);
                }
                finally
                { }
                return FTemp;
            }
        }
        public XPathNodeIterator this[string []NodeName]
        {
            get
            {
                try
                {
                    XPathNavigator FNavigator = FDocument.CreateNavigator();
                    XPathNodeIterator FNodes = FNavigator.Select(CreateXPath(NodeName));
                    //FNodes = FDocument.SelectNodes(CreateXPath(NodeName));
                    return FNodes;
                }
                finally
                {}
            }
        }
        #endregion

        #region Public Properties
        public XmlDocument Document
        {
            get { return FDocument; }
        }
        public string FileName
        {
            get { return FFileName; }
            set { FFileName = value; }
        }
        #endregion

        #region Public Metheods
        public XPathNavigator CreateXPathNav()
        {
            XPathDocument FPathDoc = new XPathDocument(FFileName);
            return FPathDoc.CreateNavigator();
        }
        public void LoadDocument(StringBuilder AXML)
        {
            FDocument = new XmlDocument();
            FDocument.LoadXml(AXML.ToString());
        }
        public void WriteEntireFile(StringBuilder AXml,bool OverWrite)
        {
            try
            {
                if (System.IO.File.Exists(FFileName) && !OverWrite)
                {
                    throw new CXMLException(true, "Specifed file exists");
                }
                FWriter = new XmlTextWriter(FFileName, Encoding.ASCII);
                FWriter.WriteRaw(AXml.ToString());
               
            }
            finally
            {
                if(FWriter != null)
                    FWriter.Close();
            }
        }
        public void Save(string FileName,bool OverWrite)
        {
            FFileName = FileName;
            if (!OverWrite && System.IO.File.Exists(FFileName))
            {
                FDocument.Save(FFileName);
            }
            else
                throw new CXMLException(true,"File already exists");
        }
        public void Save(bool OverWrite)
        {
           Save(FFileName,OverWrite);
        }
        #endregion

        #region Private Methods
        private void CreateDocument()
        {
            if (System.IO.File.Exists(FFileName))
            {
                FDocument = new XmlDocument();
                FDocument.Load(FFileName);
            }
            else
                throw new CXMLException("File doesnt exists");
        }
        private void DisposeDocument()
        {
            FDocument = null;
        }
        private string CreateXPath(string[] AList)
        {
            StringBuilder FTemp = new StringBuilder("");
            for (int i = 0; i < AList.Length; i++)
            {
                FTemp.Append("/" + AList[i]);
            }
            return FTemp.ToString();
        }
        #endregion
    }
    #endregion

    #region Custom XML Exception
    public class CXMLException : ApplicationException
    {
        bool FHandle = false;
        public CXMLException(bool Handled, string Message)
            : base(Message)
        {

            FHandle = Handled;
        }
        public CXMLException(string Message)
            : base(Message)
        {
        }
        public override string Message
        {
            get
            {
                return base.Message + "--Custom XML Exception";
            }
        }
        public bool Handled
        {
            get { return Handled; }
        }
    }
    #endregion
}

0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17902744
Hello, did this help ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XAML: Layout 8 25
Allow space in this pattern 2 48
Class object 2 27
Name space syntax error 12 43
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now