Solved

Searching/Editing XML using Linq

Posted on 2013-11-05
3
364 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
Comment Utility
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
Comment Utility
Thank you so much for your answer.  This is exactly what I was looking for.
0
 

Author Comment

by:efryevt
Comment Utility
Question closed.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now