使用 linq to XML 在 XML 文件中进行非 ASCII 字符搜索的问题
我正在使用以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在我看来是错误的:
为什么
Request.QueryString
没有由 ASP.NET 应用适当的解码?我建议您将问题分成两半:
我希望您能够直接使用
Request.QueryString("DbName")
。This looks wrong to me:
Why wouldn't
Request.QueryString
already have had appropriate decoding applied by ASP.NET?I suggest you split the problem into two halves:
Request.QueryString("DbName")
as integersI would expect you to just be able to use
Request.QueryString("DbName")
directly.我创建了一个包含以下内容的测试网页:
它是代码隐藏的:
当我单击
Test
链接时,lblTest 文本变为 True — 因此,查询会按预期找到该元素。除了 Jon Skeet 的解决方案可能解决该问题外,您还可能传递错误的
inspectionId
参数值,从而导致搜索失败。I've created a test web page with the following content:
It's code-behind:
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.