query with c# and xml

Asked

Viewed 34 times

-2

Hello,

I’m trying to do a c# query using xml but this gives me an error.

someone could help me?

code:

        private void button1_Click(object sender, EventArgs e)
    {
        XDocument doc = XDocument.Load(@"C:\..\WindowsFormsApp10\stores.xml");
        var xpath = "//store[Color='Pink']";
        var result = doc.XPathEvaluate(xpath);
        textBox1.Text = result.ToString();




    }

error: this appears in textbox: "System.xml.Xpath.Xpathevaluator+d__1`1[System.Object]"

xml:

<stores>
<store rollNumer="170">
    <Name>Jonh</Name>
        <Color>Pink</Color>
    <Sell>Sugar</Sell>
</store>

<store rollNumer="120">
    <Name>Tedy</Name>
        <Color>Brown</Color>
    <Sell>Rice</Sell>
</store>

Thank you

  • without collecting the xml there is very difficult to help..

  • What you want to appear in the textbox?

  • Managed to solve your problem? What you want to put in the textbox?

1 answer

2

For framework documentation, the method XPathEvaluate returns an object that can be a bool, one double, one string or a IEnumerable<T>.

An Object that can contain a bool, a double, a string, or an Ienumerable.

Whereas you take yours result and puts in a TextBox, I assumed you were expecting a string. So you could do var result = doc.XPathEvaluate(xpath) as string;, but without knowing XML it is really difficult to check if there is another problem.

Complete code:

private void button1_Click(object sender, EventArgs e)
{
    XDocument doc = XDocument.Load(@"C:\Users\cesar\source\repos\WindowsFormsApp10\WindowsFormsApp10\stores.xml");
    var xpath = "//store[Color='170']";
    var result = doc.XPathEvaluate(xpath) as string;
    textBox1.Text = result;
}
  • xml is like this code <Stores> <store rollNumer="170"> <Name>Jonh</Name> <Color>Pink</Color> <Sell>Sugar</Sell> </store> <store rollNumer="120"> <Name>Tedy</Name> <Color>Brown</Color> <Sell>Rice</Sell> </store> </Stores> and I’ve also changed Color='Pink' but not yet Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.