-2

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):">
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
user3148099
  • 1
  • 1
  • 1
  • You really shouldn't do `catch (Exception ex)` - https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ – Enigmativity Oct 06 '16 at 03:59
  • If you've resolved the XPath invalid token error, why not update the title of your question to something up-to-date? – kjhughes Oct 06 '16 at 04:05
  • There is no element named `table1_Group1_ Hotel_Long_NM_1`. Its name is `table1_Group1`. It has an attribute named `Hotel_Long_NM_1`. You can leave the `Hotel_Long_NM_1` out of the XPath. – JLRishe Oct 06 '16 at 04:21
  • @kjhughes S/he didn't really resolve it, just found a way to get it to stop showing up. – JLRishe Oct 06 '16 at 04:21
  • @JLRishe: Acknowledged, just meant that title should reflect *current* problem, not *past*, (resolved) part of the problem. (OP has multiple problems, including at least [what you've pointed](http://stackoverflow.com/questions/39886731/xml-c-sharp-path-has-an-invalid-token?noredirect=1#comment67061355_39886731) out and [what I've pointed out](http://stackoverflow.com/a/39887310/290085).) – kjhughes Oct 06 '16 at 12:45

1 Answers1

0

Two immediately obvious problems:

  1. Your XML is not well-formed; it has unescaped & characters in xsi:schemaLocation.

    Resolution: Replace & with &amp; in xsi:schemaLocation.

  2. Your XPath is ignoring the default namespace.

    Resolution: See Using Xpath With Default Namespace in C#

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I am trying to use the xmlnamespacemanager but I am getting an error. – user3148099 Oct 16 '16 at 02:00
  • When I type the new xmlnamespacemanager I am getting the object reference not set to an instance of an object. – user3148099 Oct 16 '16 at 02:01
  • XmlNamespaceManager nmgr = new XmlNamespaceManager(doc.OwnerDocument.NameTable); nmgr.AddNamespace("x", doc.OwnerDocument.DocumentElement.NamespaceURI); XmlNodeList RoomTypesList = doc.SelectNodes(@"/x:outerelement/x:innerelement",nmgr); – user3148099 Oct 16 '16 at 02:03