473,379 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Update an Existing Element Using Linq

I am trying to write an application among which one of the functions is to
determine the number of unique extensions found in a directory and all of
its sub directories. I am trying to use Linq to XML to do this.

Below is the code being used to accomplish what I am trying to do. In the
"if" statement, I am trying to update the <Count></CountElement to
"TESTING". However what I will really want is to add 1 to the existing
numerical value.

Your help is greatly appreciated!
System.IO.FileInfo myCurrentFile;
System.Xml.Linq.XElement myFileCountCurrentElement;

var Extensions = new XElement("Count", (from Extension in
_myFileExtensionCount.LinqXDocument.Descendants("E xtension") where
Extension.Element("Name").Value == myCurrentFile.Extension.ToLower()
select new
{
Name =
Extension.Element("Name").Value,
Count =
Extension.Element("Count").Value,
}));
// An unaccounted file extension has been found. Let's add it.
if (Extensions.IsEmpty == true)
{
myFileCountCurrentElement = _myFileExtensionCount.LinqXDocument.Root;
myFileCountCurrentElement =
_myFileExtensionCount.AppendXmlElement(myFileCount CurrentElement,
"Extension", null);
_myFileExtensionCount.AppendXmlElement(myFileCount CurrentElement,
"Name", myCurrentFile.Extension.ToLower());
_myFileExtensionCount.AppendXmlElement(myFileCount CurrentElement,
"Count", "1");
}
else
{
Extensions.SetValue("TESTING");
}

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>

Oct 30 '08 #1
5 5293
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/
Oct 30 '08 #2
I do not want to update every <Count></Countfor every extension. I just
want to udpate the <Count></Countonly if the <Name></Nameapplies for the
current extension being looked at.

For example:

<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>

Oct 30 '08 #3
Edwin wrote:
I do not want to update every <Count></Countfor every extension. I
just want to udpate the <Count></Countonly 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/
Oct 30 '08 #4
Excellent! It worked!

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.

Thanks for your help!

Oct 30 '08 #5
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/
Oct 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: yinjennytam | last post by:
Hi all, I'm new to .NET and XML and I have a question. Given an XML file, I want to navigate its content and look for one or two particular elements to get their values. At this point, it...
3
by: thomas | last post by:
I want to use a priority_queue like STL data structure. But I found that priority_queue cannot update element value: only pop/ push is supported. I'm using priority_queue to implement the prim...
5
by: Nilla | last post by:
hi, I'm trying to write the simplest db application using C#, ASP and LINQtosQL but cannot get the Update function to work. I have used the codemodel from a sample called IntroToLinq. My Insert...
4
by: karthik25 | last post by:
Hi All, I have a problem in finding control in a dynamically created updated panel. I have given the code below. Following is just a starting effort in a completely dynamic user control. I am...
0
by: mesut | last post by:
Hi, I'm using LINQ.. and I would like to understand if it's a bug or not in LINQ... I've a situation like: I'm reading all records via "Select" Statement and a specific select statment #1...
5
by: Edwin | last post by:
I am trying to write an application among which one of the functions is to determine the number of unique extensions found in a directory and all of its sub directories. I am trying to use Linq to...
1
by: Sergey Topychkanov | last post by:
I have SQL SERVER 2008 Express and C# Express 2008 - both sp1. I can remove and update data in my database only thru direct SQL query thru database explorer, but quite unable to do it through...
7
by: shanthidiana | last post by:
hi All, I am new to dot net... i am learning it and I am doing my masters project in c# dot net with sql server 2000 backend... The issue i am having now is... i am having a gridview, upon...
1
by: nadavgg | last post by:
Hi, I have recently developed a C# application using Linq. I am getting from an external database a list of profiles I need to process, some are new and some are already in the database, and need to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.