Solved

Create Class to parse XML file

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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