This method gets the name value pair based on the first two XML attributes of every node - CSharp System.Xml

CSharp examples for System.Xml:XML Attribute

Description

This method gets the name value pair based on the first two XML attributes of every node

Demo Code


using System.Diagnostics;
using System.Collections.Specialized;
using System.Xml;
using System.Data;
using System;//from   w w w  .  ja v a 2  s .  c om

public class Main{
        /// <summary>
      /// // This method gets the name value pair based on the first two attributes of every node
      /// </summary>
      /// <param name="nodeList"></param>
      /// <returns></returns>
      public static NameValueCollection GetNameValuePair( XmlNodeList nodeList ) 
      { 
         NameValueCollection nameVal = new NameValueCollection(); 
         if ( nodeList == null ) 
            return null; 
            
         // get parameter names
         XmlNode node = nodeList.Item( 0 ); 
         if ( node == null ) 
            return null; 
            
         XmlAttributeCollection attrCollection = node.Attributes; 
         if ( attrCollection == null ) 
            return null;
         if ( attrCollection.Count < 2 ) 
            return null;
            
         string attrName1 = null, attrName2 = null; 
         // read all nodes in nodelist and extract first two attributes
         foreach ( XmlNode n in nodeList ) 
         { 
            attrName1 = n.Attributes[0].Value; 
            attrName2 = n.Attributes[1].Value; 
            nameVal.Add( attrName1, attrName2 ); 
         }
         return nameVal; 
      }
}

Related Tutorials