Solved

Create Class to parse XML file

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

15 Experts available now in Live!

Get 1:1 Help Now