Acquiring multiple attributes from .xml file in c#

I have an .xml file with the structure below. I am wanting to acquire the attribute value, 0.05 and so on, for specific EndPointChannelID's. I am currently able to get the value but it is for every EndPointChannelID instead of for a desired one. Another twist is that the readings are not always going to be 6. How can I achieve only storing the values from the desired EndPointChannelID? Any suggestions will be greatly appreciated!

<Channel ReadingsInPulse="false"> <ChannelID EndPointChannelID="5154131" /> <ContiguousIntervalSets> <ContiguousIntervalSet NumberOfReadings="6"> <TimePeriod EndRead="11386.22" EndTime="2013-01-15T02:00:00Z"/> <Readings> <Reading Value="0.05" /> <Reading Value="0.04" /> <Reading Value="0.05" /> <Reading Value="0.06" /> <Reading Value="0.03" /> <Reading Value="0.53" /> </Readings> </ContiguousIntervalSet> </ContiguousIntervalSets> </Channel>

Below is the current code I have to find the Value.

XmlReader reader = XmlReader.Create(FileLocation); while (reader.Read()) { if((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Reading")) { if (reader.HasAttributes) { MessageBox.Show(reader.GetAttribute("Value")); } } }


Continuing with XMLReader path, you can do it by setting up a result list, wait for the desired channel ID, start collecting the values, and then end collecting them when the desired channel ID tag ends:

var values = new List<string>(); var collectValues = false; var desiredChannelId = "5154131"; while (reader.Read()) { if((reader.NodeType == XmlNodeType.Element)) { if (reader.Name == "ChannelID" && reader.HasAttributes) { collectValues = reader.GetAttribute("EndPointChannelID") == desiredChannelId; } else if (collectValues && reader.Name == "Reading" && reader.HasAttributes) { values.Add(reader.GetAttribute("Value")); } } }


Your code is a bit too simple. You need to read line by line and first match on EndPointChannelId. Set a flag to make it clear that you have the correct ChannelId, then, when that condition is met read the Value attributes. You'll need an array to save them into. An ArrayList would be ideal since it is of variable length.


It can be easily done using LINQ to XML:

// load document into memory var xDoc = XDocument.Load("Input.txt"); // query the document and get List<decimal> as result List<decimal> values = (from ch in xDoc.Root.Elements("Channel") where (int)ch.Element("ChannelID").Attribute("EndPointChannelID") == 5154131 from r in ch.Descendants("Reading") select (decimal)r.Attribute("Value")).ToList();


  • Google API BatchRequest: An established connection was aborted by the software in your host machine
  • How to get top 'n' percentage values
  • Is it possible to detect if a Stream has been closed by the client?
  • pandas groupby apply on multiple columns to generate a new column
  • How to update gui in c# wpf from asynchronous method callback
  • Goldberg's log1p vs. gsl_log1p
  • Faster way of converting a variable length list into a data frame in R
  • need database hourly data before two months
  • Estimating simultaneous Azure Appfabric Cache Connections
  • Assign txt file data to struct node in linked list
  • I can't call my method from class to form
  • Round each number in a Python pandas data frame by 2 decimals
  • Grabbing data from certain files
  • how to fill triangle in swift using Core graphics
  • solving a sparse non linear system of equations using scipy.optimize.root
  • Python/Flask: How to tell how long a user spends on a page? (Data entry/time log app)
  • Read asynchronously data from NetworkStream with huge amount of packets
  • determining internet speed in android
  • Using XmlReader get Unauthorized WebException
  • Reorder stacks in horizontal stacked barplot (R)
  • Design Paradigm for instantiating object from XML file
  • Convert int 60 to a time value of 60 minutes
  • How to use JAXB with HTML?
  • MySQL: select last full 5 minute interval
  • SoX running slow using a ProcessBuilder
  • View Azure WebJob Schedule in Portal?
  • LINQ to Entities does not recognize the method 'Int64 Max(Int64, Int64)' method, and this
  • Invalid access key error using credentials redeemed from an amazon open id token
  • PHP: When would you need the self:: keyword?
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • reshape alternating columns in less time and using less memory
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?