XML Utility

 public class XmlUtility
    {
        private XDocument doc = null;

        public XmlUtility(string path)
        {
            doc = XDocument.Load(path);
        }

        /// <summary>
        /// Get the content of a specified element
        /// </summary>
        /// <param name="elementName">element name</param>
        /// <returns>element content</returns>
        public string GetElementContent(string elementName)
        {
            IEnumerable<XElement> query = doc.Descendants(elementName);
            return query.First<XElement>().Value.Trim();
        }

        /// <summary>
        /// Get the content of an element whose attribute has a specified value
        /// </summary>
        /// <param name="elementName">element name</param>
        /// <param name="attributeName">attribute name</param>
        /// <param name="attributeValue">attribute value</param>
        /// <returns>the element content</returns>
        public string GetAttributedElementContent(
            string elementName, string attributeName, string attributeValue)
        {
            IEnumerable<XElement> query = doc.Descendants(elementName).Where(
                r => string.Equals(r.Attribute(attributeName).Value, attributeValue,
                    StringComparison.InvariantCultureIgnoreCase));
            return query.First<XElement>().Value.Trim();
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章