In the following code where I want with an XML file complete the attributes an object I have created:
private void btnLoadRooms_Click(object sender, EventArgs e)
{
try
{
string filePath = string.Empty;
OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user
{
filePath = file.FileName;
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNodeList RoomTypesList = doc.SelectNodes("/Room_Types/table1_Group1_Collection/table1_Group1_ Hotel_Long_NM_1/table1_Group2_Collection");
List<RoomTypes> lista = new List<RoomTypes>();
RoomTypes RT = new RoomTypes();
foreach(XmlNode xn in RoomTypesList)
{
RT = new RoomTypes();
RT.bedding = xn["Bed_Qty_Cd_1"].InnerText;
RT.bedding += " ";
RT.bedding += xn["Bed_Typ_Desc_1"].InnerText;
RT.guest = xn["Guest_Limit_1"].InnerText;
RT.roomCode = xn["Rm_Typ_CD"].InnerText;
RT.roomName = xn["Rm_Typ_NM_1"].InnerText;
}
lblError.Text = "Room types loaded.";
}
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
This is the XML file with the different nodes. After debugging what I can see is that one of the nodes have a space just after hotel:
able1_Group1_ Hotel_Long_NM_1 If I remove the space just before hotel there is no invalid token error but the nodelist is empty.
<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="RoomTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="RoomTypes" xsi:schemaLocation="RoomTypes http://shprltxrpweb01.sgdcprod.sabre.com/ReportServer?%2FRedX_Reports%2FRoomTypes&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID=nhe3flbhhs30yd2uj3vfw355&rc%3ASchema=True">
<Room_Types>
<table1_Group1_Collection>
<table1_Group1 Hotel_Long_NM_1="New Cumberland Hotel">
<table1_Group2_Collection>
<table1_Group2 Rm_Typ_Level="Hotel Level" Rm_Typ_NM_1="No Smoking Room with One King Bed Access" Rm_Typ_CD="HNK">
<table1_Group3_Collection>
<table1_Group3>
<Detail_Collection>
<Detail Component_Suite="No" Rolling_Inventory="0" Rm_Typ_Grp_Nm_And_Level="HNK (Chain Level)" Default_Room_Supplement="+0.00 USD" Rm_Typ_Qty_1="5" Guest_Limit_1="2" Rm_Class_Desc_1="Handicap Access" Bed_Qty_Cd_1="1" Bed_Typ_Desc_1="King" Default_Rm_Typ_Short_Desc_1="No Smoking Room with One King Bed Accessible." PMS_Rm_Typ_CD_1="HNK" Hotel_Rm_Serv_CD_1=" ()" Extra_Long_Description="() Extra Long Description:" Long_Description="() Long Description:" Short_Description="() Short Description:"/>
</Detail_Collection>
</table1_Group3>
<table1_Group3 Type="Channel Description(s):">