C#XML文档帮助

本文关键字:帮助 文档 C#XML | 更新日期: 2023-09-27 18:25:04

我有以下XML文件。

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

在我的C#代码中,我在XML文档中读取了如下

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

我可以成功地将SOURCE_FOLDER值和SERIAL读取到变量中,但是当我在尝试读取NETWORK_DEVICE字段时得到不正确的值时。我对network_device_name的预期结果应该是network_device_name=eth1,但我得到了network_device和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();
}

如何仅检索网络名称的eth1?

C#XML文档帮助

尝试

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

试试这个:

//THE_SETTINGS/NETWORK_DEVICE/text()