Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Searching/Editing XML using Linq

Posted on 2013-11-05
3
Medium Priority
?
389 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 64

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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