Method to obtain an XML element within an XMLDocument, given the element name. - CSharp System.Xml

CSharp examples for System.Xml:XML Document

Description

Method to obtain an XML element within an XMLDocument, given the element name.

Demo Code

/*//from  w w w.  ja  v a  2  s .  c  om
 * Copyright (C) 2006-2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Security.Cryptography;
using System.Data;
using System.ComponentModel;
using System.Security;
using System.Collections;
using System.Configuration;
using System.Security.Principal;
using System.IO;
using System.Net;
using System.Collections.Specialized;
using System.Web;
using System.Xml;
using System.Globalization;
using System;

public class Main{
        /// <summary>
        /// Method to obtain an XML element within an XMLDocument,
        /// given the element name.
        /// </summary>
        /// <param name="doc">Xml Document</param>
        /// <param name="name">element name to be found</param>
        /// <returns></returns>
        public static XmlNode FindOnly(XmlDocument doc, String name)
        {
            XmlNodeList list = FindAllElements(doc, name);
            return list.Item(0);
        }
        /// <summary>
        /// Method to obtain an XML element within an XML string,
        ///  given the element name.
        /// </summary>
        /// <param name="xml">The xml to be searched</param>
        /// <param name="name">element name</param>
        /// <returns></returns>
        public static XmlNode FindOnly(String xml, String name)
        {
            XmlDocument doc = new XmlDocument();
            doc.InnerXml = xml;
            return FindOnly(doc, name);
        }
}

Related Tutorials