肯定有更好的办法... [英] There MUST be a better way...
问题描述
对于
Search.Response的XML / XSD,我不得不诉诸于此...
请告诉我还有更好的方法!
private void ExecuteAQuery(string
strLookingForWhat)
{
//创建区域服务对象
SearchWebService.QueryService
queryService = CreateSearchService();
const string keywordQueryTemplate
=" <?xml version = \" 1.0 \"编码= \" UTF-8\" ?>< QueryPacket
xmlns = \" urn:Microsoft.Search.Query \"修订版= \" 1000
\"><查询
domain = \" QDomain \">< SupportedFormats>< ;格式> urn:M icrosoft。
Search.Response.Document.Document< / Format>< / SupportedFormat
s>< Context>< QueryText language = \" en-US \"
type = \" STRING \"> query_text_placeholder< / QueryText>< / Context
After much futzing about with the XML/XSD for the
Search.Response, I had to resort to this...
PLEASE Tell me there is a better way!
private void ExecuteAQuery(string
strLookingForWhat)
{
// create the area service object
SearchWebService.QueryService
queryService = CreateSearchService();
const string keywordQueryTemplate
= "<?xml version=\"1.0\" encoding=\"utf-8\" ?><QueryPacket
xmlns=\"urn:Microsoft.Search.Query\" Revision=\"1000
\"><Query
domain=\"QDomain\"><SupportedFormats><Format>urn:M icrosoft.
Search.Response.Document.Document</Format></SupportedFormat
s><Context><QueryText language=\"en-US\"
type=\"STRING\">query_text_placeholder</QueryText></Context
< ; / Query>< / QueryPacket>" ;;
</Query></QueryPacket>";
const string SQLQueryTemplate
="<?xml version = \" 1.0 \\ \\"编码= \" UTF-8\" ?>< QueryPacket
xmlns = \" urn:Microsoft.Search.Query \"修订版= \" 1000
\"><查询域名= \" s2003e:81
\">< SupportedFormats> ;<格式>瓮:Microsoft.Search。回复。
Document.Document< / Format>< / SupportedFormats>< Context>< Quer
yText language = \" en-US \" ;
type = \" MSSQLFT \"> query_text_placeholder< / QueryText>< / Contex
t>< Range>< StartAt> 1< ; / StartAt>< Count> 5< / Count>< / Range>< / Quer
y>< / QueryPacket>" ;;
string queryString =
keywordQueryTemplate.Replace(" query_text_placehold er",
strLookingForWhat);
//不工作(内部系统
返回错误):
string queryString2 =
SQLQueryTemplate.Replace(" query_text_placeholder",
strLookingForWhat);
//初始结果:
string queryResults = null;
try
{
queryResults =
queryService.Query(queryString);
}
catch(异常ExSheesh)
{
throw(ExSheesh);
}
//
//将返回的XML加载到
文档 - 首先创建一个新的XML文档,
//应用Response.XSD架构,
然后从收到的文本加载XML:
//
XmlDataDocument NewXmlDoc = new
XmlDataDocument();
//导致错误:
//NewXmlDoc.DataSet.ReadXmlSchema
(" e:\\SPSSites\\SPSLive\\Response.xsd");
//NewXmlDoc.DataSet.ReadXmlSchema
(" /Response.xsd");
NewXmlDoc.LoadXml(queryResults);
string XmlDocTableName =
NewXmlDoc.NameTable.ToString();
//
//获取状态:
/ /
XmlNodeList
Response_StatusNodeList = NewXmlDoc.GetElementsByTagName
(" Status");
string ActualStatus =
Response_StatusNodeList [0] .ChildNodes [0] .Value;
XmlElement TheRootElement =
NewXmlDoc.DocumentElement;
XmlNodeList RangeNodeReference =
NewXmlDoc.GetElementsByTagName(" Range");
XmlNodeList
RangeNodeContentsReference = RangeNodeReference
[0] .ChildNodes;
DataSet PsuedoResultsDS = new
DataSet();
DataTable PsuedoResultsTable = new
DataTable(" SearchResults");
//
//添加列:
//
DataColumn PRTitle = new DataColumn
(" Title",System .Type.GetType(" System.String"));
DataColumn PRLinkUrl = new
DataColumn(" LinkUrl",System.Type.GetType(&System) .s tring));
DataColumn PRFileExt = new
DataColumn(" FileExt",System.Type.GetType(" System.S tring"));
DataColumn PRDescription = new
DataColumn(" Description",System.Type .GetType
(" System.String"));
DataColumn PRFileDate = new
DataColumn(" FileDate",System.Type .GetType
(" System.DateTime"));
PsuedoResultsTable.Columns.Add
(PRTitle);
PsuedoResultsTable.Columns.Add
(PRLinkUrl);
PsuedoResultsTable.Columns.Add
(PRFileExt);
PsuedoResultsTable.Columns.Add
(PRDescription);
PsuedoResultsTable.Columns.Add
(PRFileDate);
PsuedoResultsDS.Tables.Add
(PsuedoResultsTable);
//
//现在检查
< Range>寻找结果集<结果>:
//
string sTotalItemsReturned ="" ;;
int iTotalItemsReturned = 0; < br $>
//
foreach(XmlNode ARangeNode in
RangeNodeContentsReference)
{
if (ARangeNode.Name
==" StartAt")
{
}
if(ARangeNode.Name
==" Count")
{
}
//
//当我们找到
< TotalAvailable>节点,这告诉我们我们有多少行要处理:
//
if(ARangeNode.Name
==" TotalAvailable")
{
//
//现在得到
返回了许多(在< Range>< TotalAvailable>中)和
//将其转换为
用于索引:
//
XmlNodeList
TotalValNode = NewXmlDoc.GetElementsByTagName
(" TotalAvailable");
sTotalItemsReturned = TotalValNode [0] .ChildNodes
[0] .InnerText;
如果
(sTotalItemsReturned!="")< br $>
{
iTotalItemsReturned = Convert.ToInt32
(sTotalItemsReturned);
}
}
//
//当我们找到
< Results>节点(应该在TotalAvailable之后),
//我们的结果在以下
节点中:
//
if(ARangeNode.Name
==" Results")
{
//
//获取指针
到行:
//
XmlNodeList
ResultRowNodes = ARangeNode.ChildNodes;
XmlElement
TheActualResultRecord =(XmlElement)ResultRowNodes [0];
//
//确保我们
得到的东西:
//
如果
(iTotalItemsReturned> 0)
{
//
//我们做了 -
现在每个代表一行 - 将数据拉出并放入
//进入
a保存到数据集的新记录:
//
for(int
x = 0; x< iTotalItemsReturned; x ++)
{
XmlNodeList ColumnsNodes =
TheActualResultRecord.ChildNodes;
DataRow NewDataRow = PsuedoResultsDS。表格
[" SearchResults"]。NewRow();
NewDataRow [" Title"] = ColumnsNodes [0] .InnerText;
string T0 = ColumnsNodes [0] .Name;
string T1 = ColumnsNodes [0] .InnerText;
NewDataRow [" LinkUrl"] = ColumnsNodes [1] .ChildNodes
[0] .InnerText;
string T2 = ColumnsNodes [1] .ChildNodes
[0] .InnerText;
NewDataRow [" FileExt"] = ColumnsNodes [1] .ChildNodes
[0] .Attributes [0] .InnerText;
string T3 = ColumnsNodes [1] .ChildNodes
[0] .Attributes [0] .InnerText ;
NewDataRow [" Description"] = ColumnsNodes
[2] .InnerText;
string T4 = ColumnsNodes [2] .InnerText;
NewDataRow [" FileDate"] = Convert.ToDateTime
(ColumnsNodes [3] .InnerText);
//
//
添加新行:
//
P suedoResultsDS.Tables [" SearchResults"]。Rows.Add
(NewDataRow);
}
}
>
}
}
dataGrid1.DataSource =
PsuedoResultsDS;
dataGrid1 .SetDataBinding
(PsuedoResultsDS," SearchResults");
}
const string SQLQueryTemplate
= "<?xml version=\"1.0\" encoding=\"utf-8\" ?><QueryPacket
xmlns=\"urn:Microsoft.Search.Query\" Revision=\"1000
\"><Query domain=\"s2003e:81
\"><SupportedFormats><Format>urn:Microsoft.Search. Response.
Document.Document</Format></SupportedFormats><Context><Quer
yText language=\"en-US\"
type=\"MSSQLFT\">query_text_placeholder</QueryText></Contex
t><Range><StartAt>1</StartAt><Count>5</Count></Range></Quer
y></QueryPacket>";
string queryString =
keywordQueryTemplate.Replace("query_text_placehold er",
strLookingForWhat);
// DOES NOT WORK (Internal System
Error Returned):
string queryString2 =
SQLQueryTemplate.Replace("query_text_placeholder",
strLookingForWhat);
// Init results:
string queryResults = null;
try
{
queryResults =
queryService.Query(queryString);
}
catch (Exception ExSheesh)
{
throw (ExSheesh);
}
//
// Load the returned XML into a
Document - first create a New XML Document,
// apply the Response.XSD schema,
then load the XML from the text received:
//
XmlDataDocument NewXmlDoc = new
XmlDataDocument();
// Causes error:
//NewXmlDoc.DataSet.ReadXmlSchema
("e:\\SPSSites\\SPSLive\\Response.xsd");
//NewXmlDoc.DataSet.ReadXmlSchema
("/Response.xsd");
NewXmlDoc.LoadXml(queryResults);
string XmlDocTableName =
NewXmlDoc.NameTable.ToString();
//
// Get the status:
//
XmlNodeList
Response_StatusNodeList = NewXmlDoc.GetElementsByTagName
("Status");
string ActualStatus =
Response_StatusNodeList[0].ChildNodes[0].Value;
XmlElement TheRootElement =
NewXmlDoc.DocumentElement;
XmlNodeList RangeNodeReference =
NewXmlDoc.GetElementsByTagName("Range");
XmlNodeList
RangeNodeContentsReference = RangeNodeReference
[0].ChildNodes;
DataSet PsuedoResultsDS = new
DataSet();
DataTable PsuedoResultsTable = new
DataTable("SearchResults");
//
// Add the columns:
//
DataColumn PRTitle = new DataColumn
("Title",System.Type.GetType("System.String"));
DataColumn PRLinkUrl = new
DataColumn("LinkUrl",System.Type.GetType("System.S tring"));
DataColumn PRFileExt = new
DataColumn("FileExt",System.Type.GetType("System.S tring"));
DataColumn PRDescription = new
DataColumn("Description",System.Type.GetType
("System.String"));
DataColumn PRFileDate = new
DataColumn("FileDate",System.Type.GetType
("System.DateTime"));
PsuedoResultsTable.Columns.Add
(PRTitle);
PsuedoResultsTable.Columns.Add
(PRLinkUrl);
PsuedoResultsTable.Columns.Add
(PRFileExt);
PsuedoResultsTable.Columns.Add
(PRDescription);
PsuedoResultsTable.Columns.Add
(PRFileDate);
PsuedoResultsDS.Tables.Add
(PsuedoResultsTable);
//
// Now check the nodes within
<Range> looking for the result set <Results>:
//
string sTotalItemsReturned = "";
int iTotalItemsReturned = 0;
//
foreach(XmlNode ARangeNode in
RangeNodeContentsReference)
{
if(ARangeNode.Name
== "StartAt")
{
}
if(ARangeNode.Name
== "Count")
{
}
//
// When we find the
<TotalAvailable> node, this tells us how many rows we have
// to process:
//
if(ARangeNode.Name
== "TotalAvailable")
{
//
// Now get how
many were returned (in <Range><TotalAvailable>) and
// convert it to
use for an index:
//
XmlNodeList
TotalValNode = NewXmlDoc.GetElementsByTagName
("TotalAvailable");
sTotalItemsReturned = TotalValNode[0].ChildNodes
[0].InnerText;
if
(sTotalItemsReturned != "")
{
iTotalItemsReturned = Convert.ToInt32
(sTotalItemsReturned);
}
}
//
// When we find the
<Results> node (should be right after TotalAvailable),
// our results are in the
nodes below:
//
if(ARangeNode.Name
== "Results")
{
//
// Get a pointer
to the Row:
//
XmlNodeList
ResultRowNodes = ARangeNode.ChildNodes;
XmlElement
TheActualResultRecord = (XmlElement)ResultRowNodes[0];
//
// Make sure we
got something:
//
if
(iTotalItemsReturned > 0)
{
//
// We did -
each now represents a Row - pull the data out and put
// it into
a new record to save to the dataset:
//
for (int
x=0; x < iTotalItemsReturned; x++)
{
XmlNodeList ColumnsNodes =
TheActualResultRecord.ChildNodes;
DataRow NewDataRow = PsuedoResultsDS.Tables
["SearchResults"].NewRow();
NewDataRow["Title"] = ColumnsNodes[0].InnerText;
string T0 = ColumnsNodes[0].Name;
string T1 = ColumnsNodes[0].InnerText;
NewDataRow["LinkUrl"] = ColumnsNodes[1].ChildNodes
[0].InnerText;
string T2 = ColumnsNodes[1].ChildNodes
[0].InnerText;
NewDataRow["FileExt"] = ColumnsNodes[1].ChildNodes
[0].Attributes[0].InnerText;
string T3 = ColumnsNodes[1].ChildNodes
[0].Attributes[0].InnerText;
NewDataRow["Description"] = ColumnsNodes
[2].InnerText;
string T4 = ColumnsNodes[2].InnerText;
NewDataRow["FileDate"] = Convert.ToDateTime
(ColumnsNodes[3].InnerText);
//
//
Add the new row:
//
PsuedoResultsDS.Tables["SearchResults"].Rows.Add
(NewDataRow);
}
}
}
}
dataGrid1.DataSource =
PsuedoResultsDS;
dataGrid1.SetDataBinding
(PsuedoResultsDS,"SearchResults");
}
推荐答案
那个代码让我的眼睛受伤。你能告诉我这是什么问题吗?
english?
-D
" David Sterling" <哒************ @ sterling-consulting.com>在留言中写道
新闻:1c ***************************** @ phx.gbl ...
that code makes my eyes hurt. Can you tell me what is the problem, in
english?
-D
"David Sterling" <da************@sterling-consulting.com> wrote in message
news:1c*****************************@phx.gbl...
经过多次使用XML / XSD for
Search.Response后,我不得不求助于此......
请告诉我有一个更好的方法!
private void ExecuteAQuery(string
strLookingForWhat)
//
//这里有很多代码。
}
After much futzing about with the XML/XSD for the
Search.Response, I had to resort to this...
PLEASE Tell me there is a better way!
private void ExecuteAQuery(string
strLookingForWhat)
{
// lots and lots of code here.
}
该代码让我的眼睛受伤。你能告诉我这是什么问题吗?
english?
-D
" David Sterling" <哒************ @ sterling-consulting.com>在留言中写道
新闻:1c ***************************** @ phx.gbl ...
that code makes my eyes hurt. Can you tell me what is the problem, in
english?
-D
"David Sterling" <da************@sterling-consulting.com> wrote in message
news:1c*****************************@phx.gbl...
经过多次使用XML / XSD for
Search.Response后,我不得不求助于此......
请告诉我有一个更好的方法!
private void ExecuteAQuery(string
strLookingForWhat)
//
//这里有很多代码。
}
After much futzing about with the XML/XSD for the
Search.Response, I had to resort to this...
PLEASE Tell me there is a better way!
private void ExecuteAQuery(string
strLookingForWhat)
{
// lots and lots of code here.
}
是的,基本上是这样的:
我从搜索服务的查询回复。我有
我想要的内部数据集(结果);我试过并试图找到
映射XSD无效的正确方法 - (包括使用
数据集)。
替代方案:我手动遍历XML节点,直到找到
结果,然后循环并将数据项移动到数据集中。
必须有更好的方法!
***通过Devdex发送 http://www.devdex.com ***
不要只是参加USENET ......获得奖励!
Yeah, basically this:
I''m getting back the Response from a query to a search service. I have
an inner data set (results) that I want; I have tried and tried to find
the correct way to map the XSD to no avail - (including using a
DataSet).
Alternative: I manually traverse through the XML Nodes until I find the
Results, then loop through and move the data items into a data set.
Gotta be a much better way!
*** Sent via Devdex http://www.devdex.com ***
Don''t just participate in USENET...get rewarded for it!
这篇关于肯定有更好的办法...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!