Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:12:12+00:00 2026-05-24T11:12:12+00:00

All, I have been writing some code to attempt to extract a collection of

  • 0

All,

I have been writing some code to attempt to extract a collection of items from an XML file using C#. I have been reading around and have decided to go down the LINQ route using

using System.Xml.XPath; 
using System.Xml.Linq;

However, due to the relative complexity of the XML hierarchy, I am getting a bit confused over how to access the relevent elements, which in this case are ‘dCost’ and ‘iCost’ from the following XML:

<?xml version="1.0" encoding="utf-8"?>
<message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  pStart="2010-01-01T12:12:12"
  pEnd="2010-01-01T12:12:12"
  fYear="2010-11"
  OrgID="21122211" xmlns="urn://www.XXXX.com/vcopdc/2011/01/v0.5">
  <episodes>
    <episode
      eKey="001448101"
      ur="1219765"
      campus="1334"
      eStart="17/01/2010 6:00:00 AM"
      eEnd="17/01/2010 11:00:00 AM"
      stream="X"
      dob="6/12/1936 12:00:00 AM"
      atsi="2"
      pCode="3075">
      <episodeCosts>
        <episodeCost
          area="A0902"
          description="General ATOMIC"
          account="GF"
          dCost="0.0393072400937604"
          iCost="0.156445858061913"
          location="101"
          sDate="17/01/2000 12:00:00 AM" />
        <episodeCost
          area="AAS2W"
          description="General ATOMIC"
          account="LAB"
          dCost="0.0169883227585181"
          iCost="0.0110702327817353"
          location="101"
          sDate="17/01/2000 12:00:00 AM" />
      </episodeCosts>
    </episode>
  </episodes>
</message>

What I have got so far is the following to read the dCost only:

var doc = XDocument.Load(strFileName); 
var values = doc.XPathSelectElements("//message/episodes/episode/episodeCosts"); 
foreach (var item in values) 
{
    foreach (var att in item.Elements("episodeCost"))
    {
        var value = att.Attribute("dCost").Value;
    }
}

which does not work, that is, it is not finding the the element I want it to, as my syntax is not quite right. I have looked at many examples but the XML is always much less embedded, so it is difficult to extend the examples. Any help would be most appreciated.

EDIT: I have now also realised that due to the fact that my XML file are so large, I will need to use something like:

using (var reader = XmlReader.Create(strFileName))
{
    while (reader.Read())
    {
        // Some code here.
    }
}

Again any help is most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:12:16+00:00Added an answer on May 24, 2026 at 11:12 am

    This is how I did this eventually. Perhaps it will help someone in the future.

    double dTotalICostMM = 0.0, dTotalDCostMM = 0.0;
    decimal dCost = Decimal.Zero, iCost = Decimal.Zero;
    using (XmlReader reader = XmlReader.Create(strFileName))
    {
        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element && reader.Name == "episodeCost")
            {
                dCost = Convert.ToDecimal(reader.GetAttribute("dCost"));
                iCost = Convert.ToDecimal(reader.GetAttribute("iCost"));
            }
        }
    
        // Add each cost to its total for the month
        dTotalDCostMM += (double)dCost;
        dTotalICostMM += (double)iCost;
    }
    

    Thanks for the help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing some code that creates a generic blog. Its features are
I have been writing some code that detects add and removal of USB devices,
i've been writing some OpenCL code lately on linux (ubuntu 10.4, ati catalyst 10.4
I am attempting to pull some information from my tnsnames file using regex. I
I have been writing C code for many years, but I recently came accross
I'm writing some code examples from How to Think Like a Computer Scientist in
I'm completely tired of all the buggy code I've been writing for all this
I have been sold on mod_wsgi and apache rather than mod_python. I have all
Answers provided have all been great, I mentioned in the comments of Alnitak's answer
I'm sure you have all been there, you take on a project where there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.