使用 linq to XML 在 XML 文件中进行非 ASCII 字符搜索的问题

发布于 2024-10-11 16:40:07 字数 1033 浏览 5 评论 0原文

我正在使用以下 linq to xml 查询搜索 XML 文件中的元素

XElement inspections = XElement.Load(new StreamReader( Server.MapPath(ResolveUrl(SelectInspection.InspectionFilePath)),Encoding.UTF8));

XElement inspection = (from elements in inspections.Elements("inspection")
                                       where elements.Element("inspectionid").Value == inspectionId.ToString()
                                       && elements.Element("databasename").Value == Encoding.UTF8.GetString(Request.ContentEncoding.GetBytes (Request.QueryString("DbName")))
                                       select elements).Single();

,并且我的 xml 文件是

<?xml version="1.0" encoding="utf-8"?>
<inspections>
   <inspection>
    <inspectionid>8</inspectionid>
    <databasename>Åker</databasename>
    <exported>false</exported>
  </inspection>
 </inspections>

尽管 Request.QueryString("DbName") 等于“Åker”,但查询不返回任何结果。

I am serching for an element in an XML file using following linq to xml query

XElement inspections = XElement.Load(new StreamReader( Server.MapPath(ResolveUrl(SelectInspection.InspectionFilePath)),Encoding.UTF8));

XElement inspection = (from elements in inspections.Elements("inspection")
                                       where elements.Element("inspectionid").Value == inspectionId.ToString()
                                       && elements.Element("databasename").Value == Encoding.UTF8.GetString(Request.ContentEncoding.GetBytes (Request.QueryString("DbName")))
                                       select elements).Single();

And my xml file is

<?xml version="1.0" encoding="utf-8"?>
<inspections>
   <inspection>
    <inspectionid>8</inspectionid>
    <databasename>Åker</databasename>
    <exported>false</exported>
  </inspection>
 </inspections>

Despite the Request.QueryString("DbName") is equal to "Åker", query does not return any result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九公里浅绿 2024-10-18 16:40:07

这在我看来是错误的:

Encoding.UTF8.GetString(Request.ContentEncoding
                               .GetBytes(Request.QueryString("DbName")))

为什么 Request.QueryString 没有由 ASP.NET 应用适当的解码?

我建议您将问题分成两半:

  • 确保 LINQ to XML 可以找到该字符串:在带有硬编码数据的控制台应用程序中执行此操作
  • 确保您可以获得正确的查询字符串:通过在 < 中记录 Unicode 代码点来执行此操作code>Request.QueryString("DbName") 作为整数

我希望您能够直接使用 Request.QueryString("DbName")

This looks wrong to me:

Encoding.UTF8.GetString(Request.ContentEncoding
                               .GetBytes(Request.QueryString("DbName")))

Why wouldn't Request.QueryString already have had appropriate decoding applied by ASP.NET?

I suggest you split the problem into two halves:

  • Make sure LINQ to XML can find the string: do that in a console app with hard-coded data
  • Make sure you can get the right query string: do that by logging the Unicode codepoints within Request.QueryString("DbName") as integers

I would expect you to just be able to use Request.QueryString("DbName") directly.

南笙 2024-10-18 16:40:07

我创建了一个包含以下内容的测试网页:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <a href="WebForm1.aspx?DbName=Åker">Test</a><br />
    <asp:Label runat="server" ID="lblTest"></asp:Label>
    </form>
</body>
</html>

它是代码隐藏的:

using System;
using System.Linq;
using System.Xml.Linq;

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><inspections>   <inspection>    <inspectionid>8</inspectionid>    <databasename>Åker</databasename>    <exported>false</exported>  </inspection> </inspections>";
        var inspections = XElement.Parse(xmlStr);
        XElement inspection = (from elements in inspections.Elements("inspection")
                               where elements.Element("databasename").Value == Request.QueryString["DbName"]
                               select elements).FirstOrDefault();
        lblTest.Text = (inspection != null).ToString();

    }
}

当我单击 Test 链接时,lblTest 文本变为 True — 因此,查询会按预期找到该元素。

除了 Jon Skeet 的解决方案可能解决该问题外,您还可能传递错误的 inspectionId 参数值,从而导致搜索失败。

I've created a test web page with the following content:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <a href="WebForm1.aspx?DbName=Åker">Test</a><br />
    <asp:Label runat="server" ID="lblTest"></asp:Label>
    </form>
</body>
</html>

It's code-behind:

using System;
using System.Linq;
using System.Xml.Linq;

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><inspections>   <inspection>    <inspectionid>8</inspectionid>    <databasename>Åker</databasename>    <exported>false</exported>  </inspection> </inspections>";
        var inspections = XElement.Parse(xmlStr);
        XElement inspection = (from elements in inspections.Elements("inspection")
                               where elements.Element("databasename").Value == Request.QueryString["DbName"]
                               select elements).FirstOrDefault();
        lblTest.Text = (inspection != null).ToString();

    }
}

When I click Test link, the lblTest text becomes True — so, query finds the element as expected.

In addition to Jon Skeet's solution which probably fixes the problem, you may pass wrong inspectionId parameter value resulting in failed search.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文