Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a XML document with attributes IP and Name and Value as Location. I want to load the XML document first and then extract the contents under name attribut and store it in a string array.

I can load the document by
Doc.Load("pathname");
but how to retrieve the values and store in a string array..
pls help me with ur code
Posted

Shaheen,

In the below code I used a double dimensional array, and the name can be get by using
C#
attributes.item[1].Value.ToString();
as you are using the XmlNode.Attributes property to get the attributes collection.


C++
file.Load("C:\\Users\\shaheen.n\\Documents\\LocationFile.xml");
XmlNodeList tree = file.GetElementsByTagName("Mapping");
string[,] strarr = new string[tree.Count,3];

foreach (XmlNode node in tree)
{
    XmlAttributeCollection attributes = node.Attributes;
    ip = attributes.item[0].Value.ToString();
    name = attributes.item[1].Value.ToString();
    location = node.ChildNodes[0].Value.ToString();

    // use the code to add to you string array, if you are using the double dimensional array, like -
    strarr[ind,0] = ip;
    strarr[ind,1] = name;
    strarr[ind,2] = location;
    ind++;
}

for(int i =0;i<strarr.length;i++)
{
    if(strarr[0]==ip)
    {
        Console.WriteLine("LOCATION corresponding to the entered IP is : {0}",strarr[ind,2]);
        Console.WriteLine("NAME corresponding to the entered IP is : {0}",strarr[ind,1]);
    }
}
 
Share this answer
 
Use Linq to Xml (using System.Xlm.Linq;). Then you can do somnething like this (you may need to tweak it a little, but you get the point):

XDocument = XDocument.Load(filename)
XElement root = xDocument.Element("IPLocation");
IEnumerable<XElement> list = root.Elements(); 
foreach (XElement element in list) 
{
    string ipAddr = element.Attribute("ip").Value;
    string location = element.Element("Mapping").Value;
}


Much less code involved...
 
Share this answer
 
v2
Comments
Richard MacCutchan 11-May-10 8:54am    
I knew I'd find a use for LINQ one of these days!
Try this -

TextReader r = new StreamReader(pathname);
while ((i=r.ReadLine())!=null)
{
strXML += i;
}
 
Share this answer
 
You probably need to use the XmlReader[^] class to extract the various fields and store them in your array.
 
Share this answer
 
Shaheen,

You can use Linq to get the array from XML.
I think the XML file is in the format -
MSIL
<?xml version="1.0" encoding="utf-8" ?>
<Locations>
  <Location Name="Location1" IP="127.0.0.1"></Location>
  <Location Name="Location2" IP="127.0.0.1"></Location>
  <Location Name="Location3" IP="127.0.0.1"></Location>
  <Location Name="Location4" IP="127.0.0.1"></Location>
  <Location Name="Location5" IP="127.0.0.1"></Location>
</Locations>



Check the sample code to get string array, copy the GetStringArray() returns the string array from the supplied url -

C#
using System.Xml.Linq;

class Program
   {
       static void Main(string[] args)
       {
           string[] strarr = GetStringArray("Locations.xml");

           foreach (string str in strarr)
           {
               Console.WriteLine(str);
           }
       }

       public static string[] GetStringArray(string url)
       {
            XDocument doc = XDocument.Load(url);

           var locations = from l in doc.Descendants("Location")
                           select (string)l.Attribute("Name");

           return locations.ToArray();
       }
   }
 
Share this answer
 
First I was given a XML document LocationFile.xml

<pre lang="msil"><?xml version="1.0" encoding="UTF-8"?>
<IPLocation>

    <Mapping ip="204.162.253.44">Haddows</Mapping>
    <Mapping ip="192.168.1.13">Others</Mapping>
    <Mapping ip="192.168.90.7">Haddows</Mapping>
    <Mapping ip="192.168.134.224">Guindy2</Mapping>
    <Mapping ip="192.168.15.17">Vadapalani</Mapping>
    <Mapping ip="192.168.128.214">AMB-4</Mapping>
    <Mapping ip="10.104.19.102">AMB-4</Mapping>
    <Mapping ip="10.100.121.67">GreamsRoad</Mapping>
    <Mapping ip="131.222.32.24">GreamsRoad</Mapping>
    <Mapping ip="10.108.26.80">OMR</Mapping>
    <Mapping ip="10.249.9.45">OMR</Mapping>
    <Mapping ip="10.101.210.11">OMR</Mapping>

</IPLocation>


Then I was asked to parse the file and store it in hash table and the output should be like when the IP is entered, the corresponding Location should be displayed.

The code I wrote was:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Collections;


namespace ConsoleApplication38
{
    class parse
    {
        static void Main(string[] args)
        {
            // creating an obj for the xmldocument
            
            XmlDocument file = new XmlDocument();
            
            // creating a hashtable
            
            Hashtable hash = new Hashtable();
            
            //declaring the variables
            
            string ip = "";
            string location = "";
            
            string st;
            
            //getting input from user
            
            Console.Write("Enter the IP address:");
            st = Console.ReadLine();
            
            // loading the xml file and extracting its contents and adding it to the hash table
            
            file.Load("C:\\Users\\shaheen.n\\Documents\\LocationFile.xml");
            XmlNodeList tree = file.GetElementsByTagName("Mapping");
            foreach (XmlNode node in tree)
            {
                XmlAttributeCollection attributes = node.Attributes;
                foreach (XmlAttribute att in attributes)
                {
                    ip = att.ChildNodes[0].Value.ToString();
                }
                location = node.ChildNodes[0].Value.ToString();
                hash.Add(ip, location);
                
            }
            // retrieving values from hashtable i.e by accepting ip as the key, the corresponding location(value} is retrieved
            
            if (hash.ContainsKey(st))
            {
             Console.WriteLine("LOCATION corresponding to the entered IP is : {0}",hash[st]);
            }
        }
    }
}


This worked well. Now my mentor wants me to add another attribute (name) in the xml file. so, now my xml file is:

MSIL
<?xml version="1.0" encoding="UTF-8" ?>
  <IPLocation>
  <Mapping ip="204.162.253.44" name="ashok">Haddows</Mapping>
  <Mapping ip="192.168.1.13" name="alok">Others</Mapping>
  <Mapping ip="192.168.90.7" name ="arvind">Haddows</Mapping>
  <Mapping ip="192.168.134.224" name ="anand">Guindy2</Mapping>
  <Mapping ip="192.168.15.17" name ="anoj">Vadapalani</Mapping>
  <Mapping ip="192.168.128.214" name ="ajith">AMB-4</Mapping>
  <Mapping ip="10.104.19.102" name="amal">AMB-4</Mapping>
  <Mapping ip="10.100.121.67" name="karthik">GreamsRoad</Mapping>
  <Mapping ip="131.222.32.24" name="jayanthi">GreamsRoad</Mapping>
  <Mapping ip="10.108.26.80" name="abirami">OMR</Mapping>
  <Mapping ip="10.249.9.45" name="anandhi">OMR</Mapping>
  <Mapping ip="10.101.210.11" name ="brindha">OMR</Mapping>
  </IPLocation>


Now, I should get the the newly added attribute 'name' from the XML file and store it in a string array. I am new to c#..I have no idea what to do..please tell me what should I add in my code..?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900