Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

Parsing Markup to Represent it as Objects

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
28 Mar 2009CPOL1 min read 18.2K   101   7  
An interesting problem is parsing a markup document to represent it as an object. This would be very helpful, for example, if you want to generate valid markup code.
using System;
using System.Collections.Generic;
using System.Text;

namespace MarkupLibrary
{
    /// <summary>
    /// This Class Represents a Markup Attribute
    /// </summary>
    public class MarkupAttribute
    {
        private string _name = "";
        /// <summary>
        /// The Attribute Name
        /// </summary>
        public string Name
        {
            set { _name = value.Trim().ToLower(); }
            get { return _name; }
        }
        private string _value = "";
        /// <summary>
        /// The Attribute Value
        /// </summary>
        public string Value
        {
            set 
            {
                string myvalue = value.Trim().ToLower();
                if (myvalue.StartsWith("\""))
                    myvalue = myvalue.Remove(0, 1);
                myvalue = myvalue.Replace(">", "");
                myvalue = myvalue.Replace("//", "");
                if (myvalue.EndsWith("\""))
                    myvalue = myvalue.Remove(myvalue.Length - 1, 1);
                _value = myvalue; 
            }
            get { return _value; }
        }

        /// <summary>
        /// Override the ToString to return the attribute in form of
        /// name="value"
        /// </summary>
        public override string ToString()
        {
            return Name + "=\"" + Value + "\"";
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Egypt Egypt
Fun Coder Smile | :) My Job is my Hobby Smile | :)

Comments and Discussions