我喜欢列出所有以某个字母开头的药物来填充自动填充文本框。这里是代码
public string[] GetCompletionList(string prefixText)
{
string rdfDat = AppDomain.CurrentDomain.BaseDirectory + "DrugRDF.rdf";
List<string> list = new List<string>();
TripleStore store = new TripleStore();
Graph rdf = new Graph();
FileLoader.Load(rdf, rdfDat, new RdfXmlParser());
store.Add(rdf);
string tmp = "^" + prefixText;
string sparqlQuery = "PREFIX mojLek: <http://www.example.org/mojLek#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "SELECT ?x"
+ "WHERE {?h mojLek:ime ?x ."
+ "FILTER regex(str(?x),"+tmp+") }";
SparqlQueryParser sparqlParser = new SparqlQueryParser();
SparqlQuery query = sparqlParser.ParseFromString(sparqlQuery);
Object results = store.ExecuteQuery(query);
if (results is SparqlResultSet)
{
SparqlResultSet r = results as SparqlResultSet;
foreach (SparqlResult res in r)
{
list.Add(res["x"].ToString().ToLower());
}
}
return list.ToArray();
}
但是,如果我用例如 A 尝试它,已经有以 AI 开头的情侣得到了这个错误
VDS.RDF.Parsing.RdfParseException: [Line 1 Column 263] The value 'A' is not valid as a QName
at VDS.RDF.Parsing.Tokens.SparqlTokeniser.TryGetQNameOrKeyword()
在 VDS.RDF.Parsing.Tokens.SparqlTokeniser.GetNextToken() 在 VDS.RDF.Parsing.Tokens.TokenQueue.InitialiseBuffer() 在 VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(SparqlQueryParserContext context) 在 VDS.RDF.Parsing.SparqlQueryParser .ParseInternal(TextReader input) at VDS.RDF.Parsing.SparqlQueryParser.ParseFromString(String queryString) at SuggestWebService.GetCompletionList(String prefixText) in d:\Suggest\App_Code\SuggestWebService.cs:line 57