tags:

views:

81

answers:

3

I have a bit of xml file named Sample.xml which is shown below

  <?xml version="1.0" encoding="ISO-8859-1"?>

<countries>

<country>
  <text>Norway</text>
  <value>N</value>
</country>

<country>
  <text>Sweden</text>
  <value>S</value>
</country>

<country>
  <text>France</text>
  <value>F</value>
</country>

<country>
  <text>Italy</text>
  <value>I</value>
</country>

</countries>

i have button named submit(button1).If i click that button i need to display the count(PartitionName="AIX") in a text box named textBox1, means How many PartitionName="AIX" is belonging to Type="NIC"

Can any one give me the c# code

I did like this,,but not able to get the answaer

private void button1_Click(object sender, EventArgs e)
    {
        XmlDocument doc1 = new XmlDocument();
        doc1.Load(@"D:\New Folder\WindowsFormsApplication3\WindowsFormsApplication3\Sample.xml");
        XmlNodeList a = doc1.GetElementsByTagName("AIX");
        textBox1.Text = a.Count.ToString();
    }
+1  A: 

Using xpath something like this:

count(vendor/Slot/Port[@Type='NIC']/Client[@PartitionName='AIX'])

But you have to modify it to support your namespaces.

[Edit - complete code since someone decided to down vote this - sigh]

Also easier and shorter code than going the Linq route for this particular case.

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("inv", "http://secon.com/Ultravendor");
int count  = doc.SelectNodes("inv:vendor/inv:Slot/inv:Port[@Type='NIC']/inv:Client[@PartitionName='AIX']", nsMgr).Count;
Mikael Svenson
Can you give me the complete coding so that i could learn
peter
Seriously, down voted? The answer is clearly a pointer to a solution for using regular xpath. The count part is xslt, but still.
Mikael Svenson
+3  A: 

here is a quick soln I arrived at using linq. hope you find it useful.

static void Main(string[] args)
    {
        XElement xElement = XElement.Load(@"C:\Labs\test.xml");

        // PartitionName="AIX" is belonging to Type="NIC"
        var count = xElement.Descendants().Where(x => x.Name.ToString().Contains("Port")) // namespaces might be used here for faster traversal..
                    .Where(x => x.HasAttributes && x.Attribute("Type").Value == "NIC")
                    .Descendants().Where(x => x.Name.ToString().Contains("Client"))
                    .Where(x => x.Attribute("PartitionName").Value == "AIX").Count();       


        string str = count.ToString();

        Console.WriteLine("Count = {0}", str);
        Console.ReadLine();              

    }
JayD
but here using linq concept i beleive,,but i am working on visual studio2005,framework 2.o
peter
this will work i think,,but i dont think so it will work for framework 2.o
peter
This will work only with .net 3.0 and above. I will try to comeup with a soln using the System.Xml..
JayD
Is there a reason you are using .NET 2.0 Peter?
AndyC
A: 

XmlDocument doc1 = new XmlDocument(); doc1.Load(@"C:\Labs\test.xml");

        XmlNodeList nodes = doc1.GetElementsByTagName("inv:Port");
        int count = 0;
        foreach (XmlNode childNode in nodes)
        {
            XmlNodeReader nodeReader = new XmlNodeReader(childNode);

            while (nodeReader.Read())
            {
                if (nodeReader.GetAttribute("PartitionName") == "AIX")
                {
                    count++;
                }               

            }               
        }       

        Console.WriteLine("Count = {0}", count);
        Console.ReadLine();     
JayD
THis is working fine,,Let me try to optimize the code,,last step
peter
cool. yup optimisation and also Exception handling i guess:)
JayD