Michael
Michael

Reputation: 45

How can I get the value from XML return boolean value?

I want to make a funtion to input value (funName) and check XML file attribute (FunName) then output XML file attribute (isEnable) boolean true or false

How can I modify this code?

My XML file

<itema>
   <itemb FunName="ABC" isEnable="true"></itemb>
   <itemb FunName="DEF" isEnable="false"></itemb>
</itema>

My Code

public bool FunEnable(string funName , string isEnable)
{
    bool result = true;
    XmlDocument xDL = new XmlDocument();
    xDL.Load("C://XMLFile2.xml"); //Load XML file

    XmlNode xSingleNode = xDL.SelectSingleNode("//itemb");
    XmlAttributeCollection xAT = xSingleNode.Attributes; //read all Node attribute            
    for (int i = 0; i < xAT.Count; i++)
    {
        if (xAT.Item(i).Name == "isEnable")
        {
            Console.WriteLine(xAT.Item(i).Value); //read we want attribute content                    
        }
    }
    return result;
}

Thanks a lot

Upvotes: 3

Views: 9140

Answers (4)

Charles Mager
Charles Mager

Reputation: 26223

This is fairly trivial using LINQ to XML. You can load the document using XDocument.Load and then get your isEnable value like so:

var result = doc.Descendants("itemb")
    .Where(e => (string)e.Attribute("FunName") == "ABC")
    .Select(e => (bool)e.Attribute("isEnable"))
    .Single();

You can see a working demo here: https://dotnetfiddle.net/MYTOl6

Upvotes: 3

MRebai
MRebai

Reputation: 5474

Well you can try this :

public static bool FunEnable(string funNam)
        {
            bool result = true;
            XmlDocument xDL = new XmlDocument();
            xDL.Load(@"C:/XMLFile2.xml"); //Load XML file
            XmlNodeList nodeList = xDL.SelectNodes("//itemb");
            foreach (XmlNode node in nodeList)
            {
                if (node.Attributes["FunName"].Value.Equals(funNam))
                {
                    result = Convert.ToBoolean(node.Attributes["isEnable"].Value);
                    break;
                }
            }
            Console.WriteLine("with funName = "+ funNam +" isEnable equal to : " + result);
            return result;
        }

Output

with funName = ABC isEnable equal to : True

Upvotes: 3

Stefan
Stefan

Reputation: 672

Well, I prefer Linq to XML..

Maybe that one works:

    public bool FunEnable(string funName, string isEnable)
    {
        bool result = true;
        XDocument xDL = XDocument.Load("C://XMLFile2.xml");
        var xSingleNode = from node in xDL.Descendants("itemb")
                          where node.Attribute("FunName").Value == funName
                          select node;

        if(xSingleNode.Count() > 0)
        {
            result = xSingleNode.ElementAt(0).Attribute("isEnable").Value == "true";
            //If there is at least one node with the given name, result is set to the first nodes "isEnable"-value
        }

        return result;
    }

Upvotes: 0

Gy&#246;rgy Kőszeg
Gy&#246;rgy Kőszeg

Reputation: 18023

var xDoc = XDocument.Load(path);
bool result = (from itemb in xDoc.Descendants("itemb")
               where itemb.Attribute("FunName").Value == funcName
               select itemb.Attribute("isEnable").Value == "true")
               .FirstOrDefault();

Upvotes: 1

Related Questions