简体   繁体   中英

how to determine count of tag

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();
    }

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();              

    }

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;

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();     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM