Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#. LINQ TO XML

Posted on 2011-09-09
5
Medium Priority
?
421 Views
Last Modified: 2012-05-12
Below is just a sample of my xml that I want to add too. What I want to do is be able to traverse through adding information to each parent foler.
In my example below folder name="national" has numberOfRecords 2 as it only has 2 as the total number of records inside that folder. Whereas folder
name outlet has a total number of records equal to seven as there are seven files under it. and so does folder name C, and so on. I want to be able
to traverse through and add a new attribute (numberOfRecords) to folders, national, outlet & C. I am trying to make this completely
generic so that whatever is added, can also be correctly calulated. I mean each folder can have as many folders as can be, each with as many files as can be.

I am using C#. LinqToXML, Basically I dont want to use the words folder and file at all, I want to do it all using LINQS alternative to XPATH AXES.

I know it sounds complex. Though I'm sure it is not to someone out there as it is a fairly common task to perform.

Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<myData>
  <items id="0">
    <folder name="C">
      <folder  name="outlet">
        <folder name="national" >
          <folder name="dir2" numberOfRecords="0"/>
          <folder name="dir3" numberOfRecords="2">
            <file name="tester"  />
            <file name="helper" />
          </folder>
          <folder numberOfRecords="5" />
            <file name="help"  />
            <file name="tester2" />
            <file name="tester3" />
            <file name="tester4" />
            <file name="tester5" />
          </folder>
        </folder>
      </folder>
    </folder>
  </items>
</myData>

Open in new window

0
Comment
Question by:metta0_3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 16

Expert Comment

by:disrupt
ID: 36514898
This link will give you an example how you can get values, remove and update.
http://www.xmlplease.com/update-xml-linq
http://stackoverflow.com/questions/670563/linq-to-read-xml-c
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36515069
You should be able to do something like the following:

using System;
using System.Linq;
using System.Xml.Linq;

namespace Application
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			XDocument xdoc = XDocument.Load("input.xml");
			
			foreach (var folder in xdoc.Descendants("folder"))
			{
				var numberOfRecords = folder.Attribute("numberOfRecords");
				
				if (numberOfRecords == null)
				{
					numberOfRecords = new XAttribute("numberOfRecords", string.Empty);
					folder.Add(numberOfRecords);
				}
				
				numberOfRecords.Value = folder.Descendants("file").Count().ToString();
			}
			
			xdoc.Save("output.xml");
		}
	}
}

Open in new window


Now I know you said you didn't want to use the words "folder" or "file", but I'm not sure how you'd avoid using either since you have other nodes that occur in your document. If you plan on including a record count for the myData and items nodes, then the above code could be modified. I'm also not sure what your requirement for discriminating which nodes get a "numberOfRecords" attribute without evaluating the name. If you can elaborate, then the code above could be modified.
0
 

Author Comment

by:metta0_3
ID: 36515870
Hi, thanks for your help. I was hoping to use XPATH axes. This is great what you have given me. Essentially folder is just an example. There is about 10 different things that can be in its place, all called different things. So I want it too just look at it as a node regardless of what it is called. Just to save code. Otherwise I have to write what you have provided for each node other than folder.

I do also want the item attribute to consist of numberOfRecords, because it is the top level element, so will store the totals. myData element is just a wrapper.

0
 

Author Comment

by:metta0_3
ID: 36515874
Its ok though, its not a problem using the nodenames I was just hoping for an alternative, I will use it your way, using logical AND, will award you kaufman once it is doing as desired.
0
 

Author Closing Comment

by:metta0_3
ID: 36515997
Right, I used your way and it resolved the issue completely. I just called every node folder and then created an attribute called type. Then just used your code and it was brilliant.

Thanks for your help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question