Select specific node value from XML using C# in ASP.Net

on Nov 13, 2020 08:03 AM

Hello all,

I created an XML document. I kind of looked at the C# settings because this is going to be a custom config script for my program. I need to have it in a specific directory which I already have, but now I am trying to get the correct nodes data. How can I choose a specific node value using XML?

Here is what I have.

I have 1 node with multiple values (paths) and another node that has the same parent node as a different I want the second ones value.

I want the second <DataBase> value. ‘PDMDataBaseName’ and all the Paths in <FilePaths>

    <UserProfile Name="UserPlus" Type="System.Boolean" Scope="User">
      <Value Profile="(Default)">True</Value>
    <DataBase Name="DataBaseName" Type="System.String" Scope="User">
      <Value Profile="(Default)">TRACKER </Value>
    <DBTable Name="FileTableName" Type="System.String" Scope="User">
      <Value Profile="(Default)">ScannedPDFtest</Value>
    <FilePaths Name="FileDirectories" Type="System.Collections.Specialized.StringCollection" Scope="User">
      <Path>\\file01\public1\Eng Files .pdf</Path>
    <DataBase Name="PDMDataBaseName" Type="System.String" Scope="User">
      <Value Profile="(Default)">TRACKER_TEST</Value>

Code to get userplus value 

var queryUser = from u in xml.Descendants("Settings")
                //where u.Element = "UserProfile"
                select u.Element("UserProfile").Value;
lblUserPlus.Text = queryUser.First();

others I been trying without luck

var queryFileTable = from ft in xml.Descendants("Settings")
                     select ft.Element("DBTable").Value;
lblFileTable.Text = queryFileTable.FirstOrDefault();

var queryFileDirs = from fd in xml.Descendants("Settings")
                    // where fd.Element("DBTable").Attribute("PDMTableName")
                    // select fd.Value;
                    select fd.Element("DBTable").Attribute("PDMTableName");
lblFileDirs.Text = queryFileDirs.ToString();



Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Nov 13, 2020 08:03 AM
Hi @bigbear,
Please try the following

How to get Node with lowest value from XML using C# .Net

It might help you.

Cheers Andrea.
on Nov 16, 2020 02:57 AM

Hi bigbear,

Use XmlDocument to read the values.

Refer below code.


using System.Xml;


XmlDocument doc = new XmlDocument();
string userProfile = doc.SelectNodes("/Configuration/Settings/UserProfile")[0].InnerText;
string dataBase = doc.SelectNodes("/Configuration/Settings/DataBase")[0].InnerText;
string dBTable = doc.SelectNodes("/Configuration/Settings/DBTable")[0].InnerText;
List<string> filePaths = new List<string>();
XmlNode path = doc.SelectNodes("/Configuration/Settings/FilePaths")[0];
for (int i = 0; i < path.ChildNodes.Count; i++)
string dataBasePDM = doc.SelectNodes("/Configuration/Settings/DataBase")[1].InnerText;