C# Read xml file and assign values to variables

Robbert van den Berg

I am trying to get the value of the item "Terminal ID" and "Current Configuration" and assign them to a variable.

I have found different examples on the internet but no one have the result what i want.

XML File:

<TerminalOverview xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Bmt.BmtSharp.WebInterface.Backend.API.App.Home.Model">
  <InfoItems>
    <InfoItem>
      <Name>Device name</Name>
      <Value/>
    </InfoItem>
    <InfoItem>
      <Name>Terminal ID</Name>
      <Value>253896528</Value>
    </InfoItem>
    <InfoItem>
      <Name>Current Configuration</Name>
      <Value>BmtVersion - 1.1.32</Value>
    </InfoItem>
    <InfoItem>
      <Name>Local Time</Name>
      <Value>15/10/2017 13:58:14</Value>
    </InfoItem>
    <InfoItem>
      <Name>Time zone</Name>
      <Value>Amsterdam</Value>
    </InfoItem>
  </InfoItems>
  <Message xmlns="http://schemas.datacontract.org/2004/07/Bmt.BmtSharp.WebInterface.Backend.API.Common.Models" i:nil="true"/>
  <Success xmlns="http://schemas.datacontract.org/2004/07/Bmt.BmtSharp.WebInterface.Backend.API.Common.Models">true</Success>
</TerminalOverview>

I want the Value of "Terminal ID" to be assigned to variable terminalID and the value of "Current Configuration" to be assigned to variable softwareVersion.

How can I achieve this?

jdweng

The code below will put all the items int a dictionary. Then you can get the id and configuration from the dictionary.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILEMNAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILEMNAME);

            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();

            Dictionary<string, string> dict = root.Descendants(ns + "InfoItem")
                .GroupBy(x => (string)x.Element(ns + "Name"), y => (string)y.Element(ns + "Value"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assign values to C# variables by reading characters from a text file

Bash read file assign each line to variables

read a file and assign key, values java

Read the values from XML file

How to read values in XML file?

How to Read values from xml file in java webservice and store them like application variables in Webservice?

Read file into a list and replace the variables with values in Python

Reading XML data using Powershell foreach and assign to the values Variables

jquery - Read data from txt file and assign to different variables

C# Xml file read

IPython: how to automagically load npz file and assign values to variables?

In a batch file, how can I assign a string of values to a list of variables?

Unable to assign values for strings when read data from file

Loop xml and store values as variables - C#

How to correctly declare/assign values for variables in c++

C# - Get values from JSON response and assign them to variables

c# assign values from a for loop to individual variables

is there a function in c# for assign two variables at once but different values?

Is it possible to read gradle variables from Android resources xml file?

read in multi line text file and assign multi-named environment variables in a batch file

read input from a file and append that values to variables using shell

bash + read variables & values from file by bash script

Read values from a file, automatically creating variables to store them

Codeigniter Array values Assign to Variables

Better method to assign values to variables

Using C# Dataset to read xml file

C# read XML File and select Nodes

c++ Read contents of xml file

Read xml file in c# wpf

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive