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

本文关键字:令牌 无效 有一个 Path Xml | 更新日期: 2023-09-27 18:11:39

在下面的代码中,我想用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文件。调试后,我可以看到其中一个节点在hotel后面有一个空间:

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

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

两个显而易见的问题:

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

    分辨率:将xsi:schemaLocation中的&替换为&amp;

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

    解析:参见在c#中使用Xpath和默认命名空间