简体   繁体   中英

problem in Non-ASCII character search in XML file using linq to XML

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM