Solved

Searching/Editing XML using Linq

Posted on 2013-11-05
3
365 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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

15 Experts available now in Live!

Get 1:1 Help Now