Solved

Create Class to parse XML file

Posted on 2006-10-27
3
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 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