Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 to linq?
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>

What I have tried:

C#
<pre><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();
Posted
Updated 16-Nov-20 0:14am

1 solution

To retrieve the database node based on its name:
C#
XElement PDMDataBaseName = xml
    .Descendants("Settings")
    .Elements("DataBase")
    .FirstOrDefault(el => (string)el.Attribute("Name") == "PDMDataBaseName");
To retrieve the file paths:
C#
IEnumerable<string> filePaths = xml
    .Descendants("Settings")
    .Elements("FilePath")
    .Where(el => (string)el.Attribute("Name") == "FileDirectories")
    .Elements("Path")
    .Select(el => (string)el);
 
Share this answer
 
Comments
Maciej Los 16-Nov-20 9:29am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900