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

Last Reply 8 months ago By dharmendr

Posted 8 months ago

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>

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

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")
                     //where
                     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();

 

 

You are viewing reply posted by: Andrea 8 months ago.
Posted 8 months ago
Hi @bigbear,
Please try the following

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

It might help you.

Cheers Andrea.