Solved

Searching/Editing XML using Linq

Posted on 2013-11-05
3
369 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
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with a If commands in MS word 2010 when using with a XML drop down box 5 37
Video Player 2017 5 22
InputLanguage 1 26
PowerShell script to remove string in xml file 8 20
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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