1

I am a .net beginner.

I am trying to update my xml file using linq.

I got stuck at the very first point of it i.e i cant grab the value from xml file using linq.

These are the controls I am using in my code:

cbBrandName -- combobox
cbProduct   -- combobox
txtQuantity -- TextBox

I am trying the below code:

XElement doc = XElement.Load(@"..\..\stock.xml");
string quantity = doc.Descendants("quantity")
                  .Select(y => y.Element("quantity").Value.Equals(txtQuantity.Text))
/*red scribbles to 'Element' in 'where'*/ 
                  .Where(x => x.Element("productname").Value.Equals(cbProduct.Text) &&
                  x.Element("brandname").Value.Equals(cbBrandName.Text)).ToString();
MessageBox.Show(quantity.ToString());

here I am trying to store the "quantity" value in quantity string so that i can manipulate it later and then again update to my xml file.

when I make .select as comment it isn't showing any errors but when i run it, instead of text it is showing some system.linq.Enumerable + ..... in the MessageBox.

EDIT:

enter image description here

when i give .toString() at the end it. It is showing error -- "Object reference not set to an instance of an object." when i run it.

Please help
Thanks in Advance.

3
  • ERROR : Error 1 'bool' does not contain a definition for 'Element' and no extension method 'Element' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) Commented Oct 8, 2012 at 7:03
  • Just show us xml format and result what you want. Commented Oct 8, 2012 at 7:20
  • I have given the link to xml format in my question. Please check Commented Oct 8, 2012 at 7:24

3 Answers 3

6
XElement doc = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml");
        string quantity = doc.Descendants("items")
        .Select(y => y.Element("quantity").Value)
        .Where(x => x.Element("productname").Value.Equals(cbProduct.text) && x.Element("brandname").Value.Equals(cbBrandName.text))
        .Single().Element("quantity").Value;

THIS WORKS

UPDATING XML VALUE

 doc.Descendants("items")
    .Where(x => x.Element("productname").Value.Equals(cbProduct.Text) && x.Element("brandname").Value.Equals(cbBrandName.Text))
    .Single().Element("quantity").SetValue(quantity);
        doc.Save(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml");
Sign up to request clarification or add additional context in comments.

8 Comments

oh of course. you are selecting bool. @Mr_Green See the update in a minute.
.Single().Value; if it is not what you want just tell me what you want to select in quantity
check your code once.. it has some errors like where is repeating instead of select. and use combobox names instead of tag names :)
yes it's hard coded you can replace those values by textBox.Text etc
Thank you. you were repeating where in your code which i edited :)
|
1

Its returning the text that you see because it's returning a collection of elements, not a single element. If you want just one element you can try to use the

YourCollection.FirstOrDefault()

method for example.

In your case:

XElement doc = XElement.Load(@"..\..\stock.xml");
var quantity = doc.Descendants("quantity")
              .Select(y => y.Element("quantity").Value.Equals(txtQuantity.Text))
/*red scribbles to 'Element' in 'where'*/ 
              .Where(x => x.Element("productname").Value.Equals(cbProduct.Text) &&
              x.Element("brandname").Value.Equals(cbBrandName.Text)).FirstOrDefault();
MessageBox.Show(quantity.ToString());

3 Comments

I tried this but still it is showing red scribbles to 'Element' in 'where'.
try using the where statement before the select statement.
It is showing red scribbles to firstOrDefault() with same error as i shown in the comment.
1

You need to get one element from collection because linq returns collection of "filtered" elements (even if it contains only one element). To do that you've choices: First(), FirstOrDefault(), Last(), LastOrDefault(), Single(), SingleOrDefault()`.

The difference between First() and FirstOrDefault() is that first one will throw ArgumentNullException if sequence contains no element, second one will return default value of collection element (null for string)

In your example:

XElement doc = XElement.Load(@"..\..\stock.xml");
string quantity = doc.Descendants("quantity")
                  .Select(y => y.Element("quantity").Value.Equals(txtQuantity.Text))
/*red scribbles to 'Element' in 'where'*/ 
                  .Where(x => x.Element("productname").Value.Equals(cbProduct.Text) &&
                  x.Element("brandname").Value.Equals(cbBrandName.Text))
                  .First();
MessageBox.Show(quantity.ToString());

3 Comments

@Mr_Green Use Where before Select.
@Mr_Green Oh, Forgive me the error should be that in Select you make IEnumerable<bool>. And bool can't be implicitly converted to string. So in select you need to remove Equalst(txtQuantity.Text). And remove .ToArray too.
ahh yes. I am such a noob. Now it is showing runtime error "Object reference not set to an instance of an object." pointing to where clause. any idea why so?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.