Edwin wrote:
> I am not sure I understand it but it did exactly what I wanted it to
> do. I now have to learn what exactly is happening.
It is not that complicated, just uses LINQ to XML properties (like Root)
and methods (like Elements("Extension")) and LINQ queries with a lambda
expression:
string exampleExt = ".docx";
XElement extension = doc.Root.Elements("Extension").Where(e
=> e.Element("Name").Value == exampleExt).FirstOrDefault();
if (extension != null)
{
extension.SetElementValue("Count", 1 +
(int)extension.Element("Count"));
}
So doc.Root accesses the root element, doc.Root.Elements("Extension")
all "Extension" child elements of the Root. Then the LINQ Where method
filters those elements with the lambda expression
e => e.Element("Name").Value == exampleExt
meaning it takes those "Extension" elements which have a "Name" child
element where the Value is equal to exampleExt.
FirstOrDefault() simply means we want only the first of those filtered
elements or null if there is noone.
Once we have found an element all we need to do is set the value of the
"Count" child element using SetElementValue by incrementing the value by
one, to do that we can cast the "Count" child element to an int and add
1. That cast works as XElement provides
http://msdn.microsoft.com/en-us/libr..._explicit.aspx
to cast do a lot of CLR types.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/