Peter H
Peter H

Reputation: 901

C# XMLDocument Assistance

I have the following XML File.

  <THE_SETTINGS>
    <SOURCE_FOLDER>\SERVER_NAME\folder1\</SOURCE_FOLDER>
    <NETWORK_DEVICE>eth1<SERIAL>A0123456</SERIAL></NETWORK_DEVICE>
  </THE_SETTINGS>

In my C# Code I read in the XML document as follows

XmlDocument xmldoc = new XmlDocument(); 
xmldoc.Load("PATH_TO_XML");

I can successfully read the SOURCE_FOLDER value and SERIAL into variables, however when I am getting the incorrect value when attempting to read the NETWORK_DEVICE field. My Expected result for the network_device_name should be network_device_name=eth1 but I'm getting the combined response of network_device and serial. I.E eth1A0123456

string source_folder_value;
string network_device_name;
string serial;
if(xmldoc.SelectSingleNode("//THE_SETTINGS/SOURCE_FOLDER") != null )
{
    //Success
    source_folder_value= xmldoc.SelectSingleNode("//THE_SETTINGS/SOURCE_FOLDER").InnerText.ToString();
}
if(xmldoc.SelectSingleNode("//THE_SETTINGS/NETWORK_DEVICE") != null )
{
    //Failed after this line network_device_name = "eth1A0123456"
    network_device_name= xmldoc.SelectSingleNode("//THE_SETTINGS/NETWORK_DEVICE").InnerText.ToString();
}
if(xmldoc.SelectSingleNode("//THE_SETTINGS/NETWORK_DEVICE/SERIAL") != null )
{
    //Success serial=A0123456
    serial= xmldoc.SelectSingleNode("//THE_SETTINGS/NETWORK_DEVICE/SERIAL").InnerText.ToString();
}

How do I only retrieve eth1 for the network name?

Upvotes: 1

Views: 48

Answers (2)

Ashok Rathod
Ashok Rathod

Reputation: 840

Try

xmldoc.SelectSingleNode("//THE_SETTINGS/NETWORK_DEVICE").FirstChild.InnerText

Upvotes: 1

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Try this:

//THE_SETTINGS/NETWORK_DEVICE/text()

Upvotes: 0

Related Questions