Solved

Create Class to parse XML file

Posted on 2006-10-27
3
174 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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