Xml C#Path有一个无效的令牌

时间:2016-10-06 02:49:09

标签: c# xml token

在下面的代码中,我想要一个XML文件,完成我创建的对象的属性:

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;
        }

    }

这是具有不同节点的XML文件。在调试之后,我可以看到其中一个节点在酒店后面有一个空格:

able1_Group1_ Hotel_Long_NM_1 如果我删除酒店之前的空格,则没有无效的令牌错误,但节点列表为空。

<?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):">

1 个答案:

答案 0 :(得分:0)

两个直接明显的问题:

  1. 您的XML格式不正确;它中有未转义的&个字符 xsi:schemaLocation

    解决方案:将&替换为&amp;中的xsi:schemaLocation

  2. 您的XPath忽略了默认命名空间。

    解决方案:请参阅Using Xpath With Default Namespace in C#

相关问题