Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XML General > .NET and XML

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-30-2008, 04:34 PM
Martin Honnen
 
Posts: n/a

Default Re: Update an Existing Element Using Linq



Edwin wrote:

> Below is the XML output that I am getting.
>
>
> <?xml version="1.0" encoding="utf-8" ?>
> - <FileExtensions>
> - <Extension>
> <Name>.m4b</Name>
> <Count>1</Count>
> </Extension>
> - <Extension>
> <Name>.docx</Name>
> <Count>1</Count>
> </Extension>
> - <Extension>
> <Name>.doc</Name>
> <Count>1</Count>
> </Extension>
> - <Extension>
> <Name>.xls</Name>
> <Count>1</Count>
> </Extension>
> - <Extension>
> <Name>.xlsx</Name>
> <Count>1</Count>
> </Extension>
> </FileExtensions>


Here is an example that adds 1 to each Count element in the sample above:

XDocument doc = XDocument.Parse(@"<FileExtensions>
<Extension>
<Name>.m4b</Name>
<Count>1</Count>
</Extension>
<Extension>
<Name>.docx</Name>
<Count>1</Count>
</Extension>
<Extension>
<Name>.doc</Name>
<Count>1</Count>
</Extension>
<Extension>
<Name>.xls</Name>
<Count>1</Count>
</Extension>
<Extension>
<Name>.xlsx</Name>
<Count>1</Count>
</Extension>
</FileExtensions>");
foreach (XElement ext in doc.Root.Elements("Extension")) {
ext.SetElementValue("Count", (int)ext.Element("Count")
+ 1);
}
doc.Save(Console.Out);

HTH

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-30-2008, 05:26 PM
Martin Honnen
 
Posts: n/a

Default Re: Update an Existing Element Using Linq

Edwin wrote:
> I do not want to update every <Count></Count> for every extension. I
> just want to udpate the <Count></Count> only if the <Name></Name>
> applies for the current extension being looked at.


Well my code was just meant as an example.
Here is a different example that updates Count for one Extension element:

XDocument doc = XDocument.Parse(@"<FileExtensions>
<Extension>
<Name>.m4b</Name>
<Count>10</Count>
</Extension>
<Extension>
<Name>.docx</Name>
<Count>7</Count>
</Extension>
<Extension>
<Name>.doc</Name>
<Count>324</Count>
</Extension>
<Extension>
<Name>.xls</Name>
<Count>98</Count>
</Extension>
<Extension>
<Name>.xlsx</Name>
<Count>45</Count>
</Extension>
</FileExtensions>");

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"));
}
doc.Save(Console.Out);


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
  #3 (permalink)  
Old 10-30-2008, 06:05 PM
Martin Honnen
 
Posts: n/a

Default Re: Update an Existing Element Using Linq

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/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 06:12 PM.
Style Developed by Epic Designz