我有Web服务,当用户输入ID时返回XML数据。如果数据库中存在ID,它将返回XML数据,现在我创建Test Project
,以供我参考,使用此SOAP Services
。
[WebMethod]
public DataTable GetAkontasById(int Id)
{
OracleConnection conn = new OracleConnection("DATA SOURCE=test-1:1521/test;USER ID=test;PASSWORD=test");
OracleDataAdapter dr = new OracleDataAdapter("Select * from AKONTAS where A_KONTO= '" + Id + "'", conn);
DataSet ds = new DataSet();
ds.Tables.Add("AKONTAS");
dr.Fill(ds, "AKONTAS");
DataTable tt = ds.Tables[0];
return tt;
}
这是我在运行Web Service
后的xml的样子
<DataTable xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="AKONTAS" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="AKONTAS" msprop:BaseTable.0="AKONTAS">
<xs:complexType>
<xs:sequence>
<xs:element name="A_KONTO" msprop:BaseColumn="A_KONTO" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="NAZIV" msprop:BaseColumn="NAZIV" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="SIFRA_RAD" msprop:BaseColumn="SIFRA_RAD" msprop:OraDbType="104" type="xs:string" minOccurs="0"/>
<xs:element name="OPSTINA" msprop:BaseColumn="OPSTINA" msprop:OraDbType="104" type="xs:string" minOccurs="0"/>
<xs:element name="MB" msprop:BaseColumn="MB" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="ULICA" msprop:BaseColumn="ULICA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="BROJ" msprop:BaseColumn="BROJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="PBROJ" msprop:BaseColumn="PBROJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="MJESTO" msprop:BaseColumn="MJESTO" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="PORESKI" msprop:BaseColumn="PORESKI" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="TRANSAKCIJ" msprop:BaseColumn="TRANSAKCIJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="INTERNET" msprop:BaseColumn="INTERNET" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="EMAIL" msprop:BaseColumn="EMAIL" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="KONTAKT" msprop:BaseColumn="KONTAKT" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="TELEFON" msprop:BaseColumn="TELEFON" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="FAKS" msprop:BaseColumn="FAKS" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="DAT_UNOS" msprop:BaseColumn="DAT_UNOS" msprop:OraDbType="106" type="xs:dateTime" minOccurs="0"/>
<xs:element name="PDVMB" msprop:BaseColumn="PDVMB" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="VRSTA_KLIJENTA" msprop:BaseColumn="VRSTA_KLIJENTA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="DRZAVA" msprop:BaseColumn="DRZAVA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<AKONTAS diffgr:id="AKONTAS1" msdata:rowOrder="0">
<A_KONTO>515534</A_KONTO>
<NAZIV>ABC</NAZIV>
<SIFRA_RAD>30017</SIFRA_RAD>
<MB>123456789000</MB>
<DAT_UNOS>2005-12-07T00:00:00+01:00</DAT_UNOS>
</AKONTAS>
</NewDataSet>
</diffgr:diffgram>
</DataTable>
现在O创建aspx
页面,其中包含作为参考Web Service.
此aspx
还需要返回XML数据,现在我停留在这里,不知道如何获取此XML数据并将其传递到aspx
页。
你建议我做什么?
protected void btnProjeri_Click(object sender, EventArgs e)
{
AkontasService.AkontasSoapClient client =
new AkontasService.AkontasSoapClient();
var rezultat = client.GetAkontasById(Convert.ToInt32(txtAkonto.Text));
txtRezultat.Text = rezultat.ToString();
}
答案 0 :(得分:1)
尝试关注linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication132
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
List<XElement> AKONTAS = doc.Descendants("AKONTAS").ToList();
var results = AKONTAS.Select(x => x.Elements().Select(y => new KeyValuePair<string, string>(y.Name.LocalName, (string)y)).ToList()).ToList();
foreach (var result in results)
{
Console.WriteLine(string.Join(",", result.Select(x => x.Key + " : " + x.Value)));
}
Console.ReadLine();
}
}
}