XML LINQ pick correct node value and how to choose others

Last Reply 13 days ago By dharmendr

Posted 16 days 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 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>

<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: dharmendr 13 days ago.
Posted 13 days ago

Hi bigbear,

Use XmlDocument to read the values.

Refer below code.

Namespaces

using System.Xml;

Code

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Configuration.xml"));
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++)
{
    filePaths.Add(path.ChildNodes[i].InnerText);
}
string dataBasePDM = doc.SelectNodes("/Configuration/Settings/DataBase")[1].InnerText;