Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i filter XmlNodeList

Tags:

c#

xml

winforms

I have a set of data xml node list, i want to filter with a particular attributes inner text, I tried with this but nothing worked. My code here and also posting the xml data

string baseName = categoryName.Split(':').Last();
            int categoryId = 0;
            string xmlFile = File.ReadAllText(Application.StartupPath + @"\EbayCategories\EbayCategories.xml");
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlFile);
            XmlNodeList nodeList = xmldoc.SelectNodes("/CategoryArray/Category[CategoryName='" + baseName + "']");
            if (nodeList.Count > 0)
            {
                var memberNames = nodeList.Cast<XmlNode>().Where(node => node.Attributes["CategoryID"].InnerText == "58193").ToList();
                categoryId = int.Parse(nodeList[0]["CategoryID"].InnerText);
            }

Here is my xml Data. I want to filter CategoryParentID = My Value.

<CategoryArray>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>20081</CategoryID>
        <CategoryLevel>1</CategoryLevel>
        <CategoryName>Antiques</CategoryName>
        <CategoryParentID>20081</CategoryParentID>
    </Category>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>37903</CategoryID>
        <CategoryLevel>2</CategoryLevel>
        <CategoryName>Antiquities</CategoryName>
        <CategoryParentID>20081</CategoryParentID>
    </Category>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>37908</CategoryID>
        <CategoryLevel>3</CategoryLevel>
        <CategoryName>The Americas</CategoryName>
        <CategoryParentID>37903</CategoryParentID>
        <LeafCategory>true</LeafCategory>
    </Category>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>162922</CategoryID>
        <CategoryLevel>3</CategoryLevel>
        <CategoryName>Byzantine</CategoryName>
        <CategoryParentID>37903</CategoryParentID>
        <LeafCategory>true</LeafCategory>
    </Category>

like image 293
Mehaboob Avatar asked Feb 01 '26 21:02

Mehaboob


1 Answers

I have done with small changes Removed Attributes

var node = nodeList.Cast<XmlNode>().Where(n => n["CategoryParentID"].InnerText == "58193").Select(x => x["CategoryID"].InnerText).SingleOrDefault();

Perfectly worked!!!

like image 115
Mehaboob Avatar answered Feb 03 '26 11:02

Mehaboob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!