?
Solved

Searching/Editing XML using Linq

Posted on 2013-11-05
3
Medium Priority
?
382 Views
Last Modified: 2016-02-10
I have some XML(see below) that I want to parse using Linq in C#.NET.  I want to:

1. Count the number of hardware nodes
2. Read the value for each CardID under each Hardware node
3. Change the CardModel value under each Hardware node

I apologize for this over simplified version of what I am trying to accomplish, but I believe this hits on all the points.

I have tried the following code to simply count the hardware nodes, but the query contains no elements.
XElement root = XElement.Load(myXMLPath);
            var query = from c in root.Elements("Hardware")
                        select c;

Open in new window

Example XML
<MyDataSet xmlns="http://tempuri.org/MyDataSet.xsd">
     <Hardware>
          <CardID>0</CardID>
          <CardPCISlot>0</CardPCISlot>
          <CardModel>130</CardModel>
     </Hardware>
     <Hardware>
          <CardID>1</CardID>
          <CardPCISlot>2</CardPCISlot>
          <CardModel>145</CardModel>
     </Hardware>
</MyDataSet>

Open in new window

0
Comment
Question by:efryevt
[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
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39626355
Hi efryevt;

When your XML document uses Namespace's you need to access all the nodes with that same Namespace as shown in code snippet.

XElement root = XElement.Load(myXMLPath);
XNamespace ns = "http://tempuri.org/MyDataSet.xsd";

var query = from c in root.Elements(ns + "Hardware")
            select c;
// Count the number of hardware nodes            
var hwCount = query.Count();
// Read the value for each CardID under each Hardware node
var cardIds = query.Elements(ns + "CardID").Select (c => c.Value ).ToList();
// Get the nodes to be changed
var modelNo = query.Elements(ns + "CardModel").ToList();

foreach (var model in modelNo)
{
    // Change the CardModel value under each Hardware node
    model.Value = model.Value + "ABC"; 
}

// At this point the XML document has CardModel value changed

Open in new window

0
 

Author Closing Comment

by:efryevt
ID: 39627211
Thank you so much for your answer.  This is exactly what I was looking for.
0
 

Author Comment

by:efryevt
ID: 39627218
Question closed.
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

801 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