Get Name Value Pair from XmlNodeList - CSharp System.Xml

CSharp examples for System.Xml:XML Node

Description

Get Name Value Pair from XmlNodeList

Demo Code

// All rights reserved.
using System.Diagnostics;
using System.Collections.Specialized;
using System.Xml;
using System.Data;
using System;//from  www  .j a  v a 2  s.c  o m

public class Main{
        // This method gets the name value pair based on the first two attributes of every node
        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