Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / Visual C++ 10.0

Linq-To-XML Style of Node Creation for C++

Rate me:
Please Sign up or sign in to vote.
4.78/5 (14 votes)
12 Apr 2016Ms-PL11 min read 43.3K   500   29  
Linq-To-XML Node Creation for Native C++
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace Elmax
{
    /// <summary>
    /// Class to access XML comment
    /// </summary>
    public class Comment
    {
        /// <summary>
        /// Default constructor
        /// </summary>
	    public Comment()
        {
            _Comment = null; 
        }
        /// <summary>
        /// Non-default Constructor
        /// </summary>
        /// <param name="xmlComment"></param>
	    public Comment(XmlComment xmlComment) 
        {
            _Comment = xmlComment;
        }
        /// <summary>
        /// Accessor for the content
        /// </summary>
        public string Content
        {
            get
            {
                return GetContent();
            }
        }

        /// <summary>
        /// Accessor for the length of the content
        /// </summary>
        public long Length
        {
            get
            {
                return GetLength();
            }
        }

        /// <summary>
        /// Accessor to determine if the CData Section is valid
        /// </summary>
        public bool Valid
        {
            get
            {
                return IsValid();
            }
        }
        /// <summary>
        /// Get the Comment content
        /// </summary>
        /// <returns></returns>
	    public string GetContent()
        {
	        if(_Comment==null)
                throw new System.InvalidOperationException("Invalid comment object");

	        return _Comment.Data;
        }
        /// <summary>
        /// Get the length of the Comment in Char size (2 bytes)
        /// </summary>
        /// <returns></returns>
	    public int GetLength()
        {
	        if(_Comment==null)
                throw new System.InvalidOperationException("Invalid comment object");

	        return _Comment.Length;
        }
        /// <summary>
        /// Delete itself
        /// </summary>
        /// <returns>true if successful</returns>
	    public bool Delete()
        {
	        if(_Comment==null)
                throw new System.InvalidOperationException("Invalid comment object");

	        XmlNode parent = _Comment.ParentNode;
	        if(parent!=null)
	        {
		        _Comment = (XmlComment)(parent.RemoveChild(_Comment));
	        }
	        else
		        return false;

	        return true;
        }
        /// <summary>
        /// Update the comment contents
        /// </summary>
        /// <param name="comment">Comment to update</param>
        /// <returns>true if successful</returns>
	    public bool Update(string comment)
        {
	        if(_Comment==null)
		        throw new System.InvalidOperationException("Invalid comment object");

	        _Comment.ReplaceData(0, comment.Length, comment);
	        return true;
        }
        /// <summary>
        /// Get internal XmlComment object
        /// </summary>
        /// <returns>Internal XmlComment object</returns>
        public XmlComment GetInternalObject()
        {
            return _Comment;
        }
        /// <summary>
        /// Check if comment is in valid state
        /// </summary>
        /// <returns>true if valid</returns>
        public bool IsValid()
        {
            if (_Comment == null)
                return false;

            return true;
        }
        /// <summary>
        /// Internal XmlComment object
        /// </summary>
        private XmlComment _Comment;
    }
}

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 Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions